繁体   English   中英

摆动-显示空白屏幕的Java程序

[英]Swing - Java program showing blank screen

我正在做妈妈建议的新程序。 应该列出我们拥有的物品和我们需要的物品。 现在我只有一个班级,即窗口创建班级,这给了我一个空白的屏幕。 我不知道为什么,但是我可能缺少一些重要的小步骤。 到目前为止的代码如下:

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_BOTH);

希望这就是你想要的

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM