Swing-Java程序显示空白屏幕
我正在做一个我妈妈建议的新节目。它应该列出我们拥有的物品和我们需要的物品。现在我只有一个类,窗口创建类,它给了我一个空白屏幕。我不知道为什么,但我可能错过了一些重要的小步骤。以下是迄今为止的代码:Swing-Java程序显示空白屏幕,java,swing,jframe,awt,Java,Swing,Jframe,Awt,我正在做一个我妈妈建议的新节目。它应该列出我们拥有的物品和我们需要的物品。现在我只有一个类,窗口创建类,它给了我一个空白屏幕。我不知道为什么,但我可能错过了一些重要的小步骤。以下是迄今为止的代码: public class ListerWindow { static JButton addToOwned = new JButton("Add Item To Owned List"); static JButton removeFromOwned = new JButton("Remove It
public class ListerWindow {
static JButton addToOwned = new JButton("Add Item To Owned List");
static JButton removeFromOwned = new JButton("Remove Item From Owned List");
static JButton addToNeeded = new JButton("Add Item To Shopping List");
static JButton removeFromNeeded = new JButton("Remove From Shopping List");
static JTextArea neededList = new JTextArea();
static JTextArea ownedList = new JTextArea();
static JFrame frame = new JFrame("Shopping Lister");
static JLabel ownedListLabel = new JLabel("Owned List");
static JLabel neededListLabel = new JLabel("Shopping List");
public static void ListerWindowCreator(String[] args) {
JPanel windowContent = new JPanel();
GridLayout gl = new GridLayout(4,3);
windowContent.setLayout(gl);
windowContent.add(ownedListLabel);
windowContent.add(neededListLabel);
windowContent.add(ownedList);
windowContent.add(neededList);
windowContent.add(addToOwned);
windowContent.add(addToNeeded);
windowContent.add(removeFromOwned);
windowContent.add(removeFromNeeded);
neededList.setEditable(false);
ownedList.setEditable(false);
ownedListLabel.setForeground(Color.BLUE);
neededListLabel.setForeground(Color.BLUE);
frame.setBackground(Color.BLACK);
removeFromNeeded.setForeground(Color.RED);
removeFromOwned.setForeground(Color.RED);
addToNeeded.setForeground(Color.GREEN);
addToOwned.setForeground(Color.GREEN);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
new ListerWindow();
ListerWindowCreator(args);
}
}
我很确定我已经得到了所有的导入,到目前为止我已经得到了javax.swing.
和java.awt.
PS:我可以在eclipse中调试吗?如何调试?您需要使用
JFrame添加windowContent
frame.add(windowContent);
对于JFrame
的确切大小,可以使用frame.pack()
而不是frame.setExtendedState(frame.MAXIMIZED\u两者)
您需要使用JFrame
frame.add(windowContent);
对于JFrame
的确切大小,可以使用frame.pack()
而不是frame.setExtendedState(frame.MAXIMIZED\u两者)代码>希望这就是你想要的
import java.awt.*;
import javax.swing.*;
public class ListerWindow{
static JButton addToOwned = new JButton("Add Item To Owned List");
static JButton removeFromOwned = new JButton("Remove Item From Owned List");
static JButton addToNeeded = new JButton("Add Item To Shopping List");
static JButton removeFromNeeded = new JButton("Remove From Shopping List");
static JTextArea neededList = new JTextArea();
static JTextArea ownedList = new JTextArea();
static JFrame frame = new JFrame("Shopping Lister");
static JLabel ownedListLabel = new JLabel("Owned List");
static JLabel neededListLabel = new JLabel("Shopping List");
public static void ListerWindowCreator(String[] args) {
JPanel windowContent = new JPanel();
GridLayout gl = new GridLayout(4,2);
windowContent.setLayout(gl);
windowContent.add(ownedListLabel);
ownedList.setText("ownedList");
windowContent.add(ownedList);
windowContent.add(neededListLabel);
neededList.setText("NeededList");
windowContent.add(neededList);
windowContent.add(addToOwned);
windowContent.add(addToNeeded);
windowContent.add(removeFromOwned);
windowContent.add(removeFromNeeded);
// neededList.setEditable(false);
// ownedList.setEditable(false);
ownedListLabel.setForeground(Color.BLUE);
neededListLabel.setForeground(Color.BLUE);
frame.setBackground(Color.BLACK);
removeFromNeeded.setForeground(Color.RED);
removeFromOwned.setForeground(Color.RED);
addToNeeded.setForeground(Color.GREEN);
addToOwned.setForeground(Color.GREEN);
frame.setContentPane(windowContent);
frame.setSize(200,200);
//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
new ListerWindow();
ListerWindowCreator(args);
}
}
希望这是你想要的
import java.awt.*;
import javax.swing.*;
public class ListerWindow{
static JButton addToOwned = new JButton("Add Item To Owned List");
static JButton removeFromOwned = new JButton("Remove Item From Owned List");
static JButton addToNeeded = new JButton("Add Item To Shopping List");
static JButton removeFromNeeded = new JButton("Remove From Shopping List");
static JTextArea neededList = new JTextArea();
static JTextArea ownedList = new JTextArea();
static JFrame frame = new JFrame("Shopping Lister");
static JLabel ownedListLabel = new JLabel("Owned List");
static JLabel neededListLabel = new JLabel("Shopping List");
public static void ListerWindowCreator(String[] args) {
JPanel windowContent = new JPanel();
GridLayout gl = new GridLayout(4,2);
windowContent.setLayout(gl);
windowContent.add(ownedListLabel);
ownedList.setText("ownedList");
windowContent.add(ownedList);
windowContent.add(neededListLabel);
neededList.setText("NeededList");
windowContent.add(neededList);
windowContent.add(addToOwned);
windowContent.add(addToNeeded);
windowContent.add(removeFromOwned);
windowContent.add(removeFromNeeded);
// neededList.setEditable(false);
// ownedList.setEditable(false);
ownedListLabel.setForeground(Color.BLUE);
neededListLabel.setForeground(Color.BLUE);
frame.setBackground(Color.BLACK);
removeFromNeeded.setForeground(Color.RED);
removeFromOwned.setForeground(Color.RED);
addToNeeded.setForeground(Color.GREEN);
addToOwned.setForeground(Color.GREEN);
frame.setContentPane(windowContent);
frame.setSize(200,200);
//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
new ListerWindow();
ListerWindowCreator(args);
}
}
是否可以使用frame.setContentPane(windowContent)
?这就是我学习的方式,有什么区别吗?JFrame\35; add
将委托给内容窗格frame.add()
与frame.getContentPane().add()相同。您当然可以使用setContentPane
。区别在于frame.add(somePanel)
将面板添加到面板中。@Radiodef,谢谢,setContentPane
也可以使用。使用frame.setContentPane(windowContent)
可以吗?这就是我学习的方式,有什么区别吗?JFrame\35; add
将委托给内容窗格frame.add()
与frame.getContentPane().add()相同。您当然可以使用setContentPane
。区别在于frame.add(somePanel)
将面板添加到面板中。@Radiodef,谢谢,setContentPane
也可以使用。