[英]Second JFrame doesn't show from first JFrame
This is a simple JFrame
application. 这是一个简单的
JFrame
应用程序。 It basically creates a new Frame
on basis of the user choice. 它基本上是根据用户选择创建一个新的
Frame
。 The first frame starts but the new one doesn't show up! 第一帧开始,但是新的帧不显示! It show the errors-
ie1 cannot be resolved
& ie2 cannot be resolved
. 它显示
ie2 cannot be resolved
ie1 cannot be resolved
& ie2 cannot be resolved
。 I want to see the new Frame
. 我想看一下新的
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. 您既没有实例化,也没有在任何地方初始化
ie1
和ie2
变量。 I can see these represent ItemEvent
references, but their scope is limited to the ItemListener
'changed' method. 我可以看到它们代表
ItemEvent
引用,但是它们的范围仅限于ItemListener
'方法。
If you are using Eclipse, it should provide you with a quick fix. 如果您使用的是Eclipse,它将为您提供快速修复。 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. 但是,如果您是我,那么我将首先开始
Java for Beginners
阅读Java for Beginners
,然后再转向AWT / Swing和SWT / JFace。
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 几件事:您不需要ItemListeners即可获得组合框的选定值,而只需
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: 并且由于您使用的是匿名内部类,因此需要确保将
jcb1
和jcb2
声明为final
,如下所示:
final JComboBox jcb1 = new JComboBox();
Also, change ie1.getItem().equals(...)
to just ie1.equals(...)
, and do the same for ie2
. 同样,将
ie1.getItem().equals(...)
更改为ie1.equals(...)
,并对ie2
进行相同的ie2
。
On another note, don't put semicolons after if statements. 另一方面,请勿在if语句后加上分号。
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. 因此,删除那些if语句之后的分号。
With all that said, I would definitely recommend going with GGrec's advice, and start reading some Java tutorials . 综上所述,我绝对建议您遵循GGrec的建议,并开始阅读一些Java教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.