This is a simple JFrame
application. It basically creates a new Frame
on basis of the user choice. The first frame starts but the new one doesn't show up! It show the errors- ie1 cannot be resolved
& ie2 cannot be resolved
. I want to see the new Frame
.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Test2 {
public static void main(String[] args) {
JFrame jf = new JFrame("Java test");
Container c = jf.getContentPane();
jf.setBounds(450, 180, 450, 450);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
JPanel jp = new JPanel();
c.add(jp);
JLabel jl = new JLabel("This is a text in a label",SwingConstants.CENTER);
jp.add(jl);
JComboBox jcb1 = new JComboBox();
jp.add(jcb1);
jcb1.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent ie1) {
ie1.getItem();
}
});
jcb1.addItem(" Select the Size ");
jcb1.addItem("100 x 100");
jcb1.addItem("200 x 200");
jcb1.addItem("300 x 300");
jcb1.addItem("400 x 400");
jcb1.addItem("500 x 500");
jcb1.addItem("600 x 600");
JComboBox jcb2 = new JComboBox();
jp.add(jcb2);
jcb2.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent ie2) {
ie2.getItem();
}
});
jcb2.addItem(" Select the Colour ");
jcb2.addItem("Blue");
jcb2.addItem("Red");
jcb2.addItem("Black");
jcb2.addItem("White");
jcb2.addItem("Yellow");
jcb2.addItem("Green");
JButton jb = new JButton("Create a new Frame");
jp.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final JFrame jf1 = new JFrame("New Frame");
Container c = jf1.getContentPane();
jf1.setVisible(true);
jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
// The Size of the frame
if (ie1.getItem().equals(" Select the Size ")) {
JOptionPane.showMessageDialog(null, "Please select the size of the Frame");
}
if (ie1.getItem().equals("100 x 100"))
;
{
jf1.setBounds(450, 180, 100, 100);
}
if (ie1.getItem().equals("200 x 200"))
;
{
jf1.setBounds(450, 180, 200, 200);
}
if (ie1.getItem().equals("300 x 300"))
;
{
jf1.setBounds(450, 180, 300, 300);
}
if (ie1.getItem().equals("400 x 400"))
;
{
jf1.setBounds(450, 180, 400, 400);
}
if (ie1.getItem().equals("500 x 500"))
;
{
jf1.setBounds(450, 180, 500, 500);
}
if (ie1.getItem().equals("600 x 600"))
;
{
jf1.setBounds(450, 180, 600, 600);
}
// The size of the Frame ends
// The colour of the frame
if (ie2.getItem().equals(" Select the Colour ")) {
JOptionPane.showMessageDialog(null, "Please select the colour of the Frame");
}
final JPanel jp1 = new JPanel();
c.add(jp1);
if (ie2.getItem().equals("Blue")) {
jp1.setBackground(Color.blue);
}
if (ie2.getItem().equals("Red")) {
jp1.setBackground(Color.red);
}
if (ie2.getItem().equals("Black")) {
jp1.setBackground(Color.black);
}
if (ie2.getItem().equals("White")) {
jp1.setBackground(Color.white);
}
if (ie2.getItem().equals("Yellow")) {
jp1.setBackground(Color.yellow);
}
if (ie2.getItem().equals("Green")) {
jp1.setBackground(Color.green);
}
// the colour of the frame ends
}
});
}
}
You are not instantiating, nor initializing the ie1
and ie2
variables anywhere. I can see these represent ItemEvent
references, but their scope is limited to the ItemListener
'changed' method.
If you are using Eclipse, it should provide you with a quick fix. But if I were you, I would start reading on Java for Beginners
first, and after that move onto the AWT/Swing and SWT/JFace stuff.
Try starting with more basic stuff. It occurs to me that the code above is a bit overwhelming for you. Good luck lil programmer.
Several things: You don't need ItemListeners to get the selected value of the combo boxes, instead just do
Object ie1 = jcb1.getSelectedItem();
Object ie2 = jcb2.getSelectedItem();
right above
if(ie1.equals(" Select the Size "))
{
JOptionPane.showMessageDialog(null,"Please select the size of the Frame");
}
And since you're using an anonymous inner class, you'll need to make sure jcb1
and jcb2
are declared final
, like so:
final JComboBox jcb1 = new JComboBox();
Also, change ie1.getItem().equals(...)
to just ie1.equals(...)
, and do the same for ie2
.
On another note, don't put semicolons after if statements.
Right:
if(ie1.equals("100 x 100"))
{
...
}
Wrong:
if(ie1.equals("100 x 100"));
{ //^
...
}
So remove the semicolons that you have after those if statements.
With all that said, I would definitely recommend going with GGrec's advice, and start reading some Java tutorials .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.