这是一个简单的JFrame
应用程序。Frame
它基本上是根据用户的选择创建一个新的。第一帧开始,但新的没有出现!它显示错误
ie1 cannot be resolved
- & ie2 cannot be resolved
。我想看新的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
}
});
}
}