-
November 19th, 2012, 01:40 PM
#1
Changing the properties of a picture
Is everything running smoothly with this code, can some please review I get errors when I try to run the program. I have attached the code, as well at the picture.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Exercise17_09 extends JFrame {
private ImageIcon icGrapes = new ImageIcon("C:\temp");
private JPanel top;
private JPanel bottom;
private JPanel text;
private JPanel align;
private JPanel txtcom;
private JPanel lblpane;
private JPanel lblpnl;
private JPanel AlignPanel;
private JLabel jlblGrapes;
private JLabel jlbltwo;
private JLabel jlblthree;
private JLabel jlblfour;
private JLabel jlblfive;
private JComboBox jcboone;
private JComboBox jcbotwo;
private JComboBox jcbothree;
private JComboBox jcbofour;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Exercise17_09 inst = new Exercise17_09();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}});
}//end main
public Exercise17_09() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
top = new JPanel();
getContentPane().add(top, BorderLayout.NORTH);
top.setPreferredSize(new java.awt.Dimension(392, 122));
{
jlblGrapes = new JLabel("Grapes", icGrapes, SwingConstants.CENTER);
jlblGrapes.setPreferredSize(new java.awt.Dimension(392,122));
jlblGrapes.setHorizontalTextPosition(SwingConstants.CENTER);
}
}//end
{
bottom = new JPanel();
GridLayout BottomPanelLayout = new GridLayout(1, 2);
BottomPanelLayout.setHgap(5);
BottomPanelLayout.setVgap(5);
BottomPanelLayout.setColumns(2);
bottom.setLayout(BottomPanelLayout);
getContentPane().add(bottom, BorderLayout.CENTER);
bottom.setPreferredSize(new java.awt.Dimension(392, 130));
{
AlignPanel = new JPanel();
BorderLayout AlignPanelLayout = new BorderLayout();
bottom.add(AlignPanel);
AlignPanel.setLayout(AlignPanelLayout);
AlignPanel.setBorder(BorderFactory.createTitledBorder(null, "Horizontal Alignment", TitledBorder.LEADING, TitledBorder.TOP));
{
lblpnl = new JPanel();
GridLayout jPanel1Layout = new GridLayout(2, 1);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
jPanel1Layout.setColumns(1);
jPanel1Layout.setRows(2);
AlignPanel.add(lblpnl, BorderLayout.CENTER);
lblpnl.setLayout(jPanel1Layout);
{
jlblfive = new JLabel();
lblpnl.add(jlblfive);
jlblfive.setText("Horizontal");
}
{
jlblfour = new JLabel();
lblpnl.add(jlblfour);
jlblfour.setText("Vertical");
}
}
{
align = new JPanel();
GridLayout AligncomboLayout = new GridLayout(2, 1);
AligncomboLayout.setHgap(5);
AligncomboLayout.setVgap(5);
AligncomboLayout.setColumns(1);
AligncomboLayout.setRows(2);
AlignPanel.add(align, BorderLayout.EAST);
align.setLayout(AligncomboLayout);
{
ComboBoxModel jcboHAModel =
new DefaultComboBoxModel(
new String[] { "LEFT", "CENTER", "RIGHT" });
jcbofour = new JComboBox();
align.add(jcbofour);
jcbofour.setModel(jcboHAModel);
}
{
ComboBoxModel jcboVAModel =
new DefaultComboBoxModel(
new String[] { "TOP", "CENTER", "BOTTOM" });
jcbothree = new JComboBox();
align.add(jcbothree);
jcbothree.setModel(jcboVAModel);
}
}
}
{
text = new JPanel();
BorderLayout TextPanelLayout = new BorderLayout();
bottom.add(text);
text.setLayout(TextPanelLayout);
text.setBorder(BorderFactory.createTitledBorder("Text Position"));
{
lblpane = new JPanel();
GridLayout Label2panelLayout = new GridLayout(2, 1);
Label2panelLayout.setHgap(5);
Label2panelLayout.setVgap(5);
Label2panelLayout.setColumns(1);
Label2panelLayout.setRows(2);
text.add(lblpane, BorderLayout.CENTER);
lblpane.setLayout(Label2panelLayout);
{
jlblthree = new JLabel();
lblpane.add(jlblthree);
jlblthree.setText("Horizontal");
}
{
jlbltwo = new JLabel();
lblpane.add(jlbltwo);
jlbltwo.setText("Vertical");
}
}
{
txtcom = new JPanel();
GridLayout TextComboLayout = new GridLayout(2, 1);
TextComboLayout.setHgap(5);
TextComboLayout.setVgap(5);
TextComboLayout.setColumns(1);
TextComboLayout.setRows(2);
txtcom.setLayout(TextComboLayout);
text.add(txtcom, BorderLayout.EAST);
{
ComboBoxModel jcboTHAModel =
new DefaultComboBoxModel(
new String[] { "LEFT", "CENTER", "RIGHT" });
jcbotwo = new JComboBox();
txtcom.add(jcbotwo);
jcbotwo.setModel(jcboTHAModel);
jcbotwo.setSelectedItem("CENTER");
}
{
ComboBoxModel jcboTVAModel =
new DefaultComboBoxModel(
new String[] { "TOP", "CENTER", "BOTTOM" });
jcboone = new JComboBox();
txtcom.add(jcboone);
jcboone.setModel(jcboTVAModel);
jcboone.setSelectedItem("CENTER");
}
}
}
jcbofour.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String A = (String)jcbofour.getSelectedItem();
if(A.equals("LEFT")) {
jlblGrapes.setHorizontalAlignment(SwingConstants.LEFT);
}
else if(A.equals("CENTER")) {
jlblGrapes.setHorizontalAlignment(SwingConstants.CENTER);
}
else if(A.equals("RIGHT")) {
jlblGrapes.setHorizontalAlignment(SwingConstants.RIGHT);
}
}
});
jcbothree.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String A = (String)jcbothree.getSelectedItem();
if(A.equals("TOP")) {
jlblGrapes.setVerticalAlignment(SwingConstants.TOP);
}
else if(A.equals("CENTER")) {
jlblGrapes.setVerticalAlignment(SwingConstants.CENTER);
}
else if(A.equals("BOTTOM")) {
jlblGrapes.setVerticalAlignment(SwingConstants.BOTTOM);
}
}
});
jcbotwo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String A = (String)jcbotwo.getSelectedItem();
if(A.equals("LEFT")) {
jlblGrapes.setHorizontalTextPosition(SwingConstants.LEFT);
}
else if(A.equals("CENTER")) {
jlblGrapes.setHorizontalTextPosition(SwingConstants.CENTER);
}
else if(A.equals("RIGHT")) {
jlblGrapes.setHorizontalTextPosition(SwingConstants.RIGHT);
}
}
});
jcboone.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String A = (String)jcboone.getSelectedItem();
if(A.equals("TOP")) {
jlblGrapes.setVerticalTextPosition(SwingConstants.TOP);
}
else if(A.equals("CENTER")) {
jlblGrapes.setVerticalTextPosition(SwingConstants.CENTER);
}
else if(A.equals("BOTTOM")) {
jlblGrapes.setVerticalTextPosition(SwingConstants.BOTTOM);
}
}
});
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
November 20th, 2012, 04:01 AM
#2
Re: Changing the properties of a picture
I noticed you have started another thread on this subject. Is this problem now resolved?
-
November 20th, 2012, 09:10 AM
#3
Re: Changing the properties of a picture
I thought I didn't successfully post the other thread, and I made an error with the code formating so it doesn't look right. The problem is when I compile the code, the applet will never show up.
-
November 20th, 2012, 10:02 AM
#4
Re: Changing the properties of a picture
The problem is when I compile the code, the applet will never show up.
It's not an applet, it's a standalone program.
Before I spend any time looking at this, which thread has the latest code in it?
-
November 20th, 2012, 11:33 AM
#5
Re: Changing the properties of a picture
This one has the updated code
-
November 20th, 2012, 12:13 PM
#6
Re: Changing the properties of a picture
You'll have to be more specific about what is wrong than "it never shows up", I can see no reason why the JFrame will not display.
BTW the comments I added to the other post are still relevant
-
November 20th, 2012, 01:54 PM
#7
Re: Changing the properties of a picture
Yes the Jframe does not display is the overall problem. When I run it on netbeans. The Jframe will not diplay, it just says build succesful and does nothing else
-
November 20th, 2012, 02:25 PM
#8
Re: Changing the properties of a picture
It runs and displays on my system.
Are you sure you are running it and not just compiling it - "build successful" sound like a compiler message and not a runtime message.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|