JFrame 无法正常工作

发布于 2024-09-05 21:41:42 字数 2410 浏览 3 评论 0原文

这让我非常生气,我已经为此工作了 2 天,打开了 2 本书并浏览了它们,但仍然无法让这个程序按照我想要的方式运行。我已经到了这样的地步:如果这没有帮助,我就会放弃。

我想要一个简单的框架应用程序。 它有一个位于顶部居中的 JComboBox。 旁边是一个足够大的文本字段,可以显示数字,例如“$49.99” 下面是显示服务条款的文本区域 下面是同意服务条款的复选框 下面是 2 个按钮“接受”和“拒绝”

我已经为此工作了 2 天,这里是编码:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);
        JPanel pane1 = new JPanel(new FlowLayout());
        JPanel pane2 = new JPanel(new FlowLayout());

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(this);
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        frame.setJMenuBar(menuBar);
        JComboBox packageChoice =  new JComboBox(tests);
        frame.add(packageChoice);


    }

     public void actionPerformed(ActionEvent e)
  {
  Object source = e.getSource();
  {
  }

}

编辑: 忘记添加第二个程序

public class JFrameWithPanel
{
    public static void main(String[] args)
    {
         JPanel panel = new Bar(new JFrame("CIT Test Program"));
    }
}

我怎样才能让它拥有我想要的一切并显示出来?因此我很困惑,现在几乎不知道框架是如何工作的。

This is making me very angry, I have worked on this for 2 days, have 2 books open and have looked through them, and STILL can't get this program to run the way I want it run. I'm getting to the point where if this doesn't help, I quit.

I want a SIMPLE Frame application.
It has a JComboBox centered at the top.
Next to it is a text field big enough to show numeric digits such as "$49.99"
Below it is a spot for a Text area showing terms of service
Below that is the checkbox agreeing to the terms of service
Below that is 2 buttons "Accept" and "Decline"

I Have worked on this for 2 days, here is the coding:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);
        JPanel pane1 = new JPanel(new FlowLayout());
        JPanel pane2 = new JPanel(new FlowLayout());

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(this);
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        frame.setJMenuBar(menuBar);
        JComboBox packageChoice =  new JComboBox(tests);
        frame.add(packageChoice);


    }

     public void actionPerformed(ActionEvent e)
  {
  Object source = e.getSource();
  {
  }

}

EDIT:
Forgot to add the second program

public class JFrameWithPanel
{
    public static void main(String[] args)
    {
         JPanel panel = new Bar(new JFrame("CIT Test Program"));
    }
}

How do I get this to have everything where I want it and show up? I'm very confused because of this and now barely even get how Frames work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以心动 2024-09-12 21:41:42

Swing 中的组件必须按一定的顺序排列。

您从 JFrame 开始。放置在 JFrame 中的唯一组件是 JMenuBar 和 JPanel。您无需在 JFrame 中添加任何其他组件。您在 JPanel 中添加组件。

这是 Nick 的代码,经过重新组织,以正确的顺序定义组件。我使用 GridLayout 因为它更快。正如 Nivas 所说,您应该使用 GridBagLayout。

public class Bar {

    private static final long serialVersionUID = 1L;

    public Bar(final JFrame frame) {
        JMenuBar menuBar = buildMenuBar();
        frame.setJMenuBar(menuBar);

        JPanel masterPanel = new JPanel(new GridLayout(2, 1));

        JPanel pane1 = new JPanel(new GridLayout(3, 1));
        pane1.add(new JLabel("Welcome to the CIT Test Program "));
        pane1.add(new JLabel("Please select which Test Package from the list below."));
        JButton button = new JButton("Click Me");
        pane1.add(button);

        JPanel pane2 = new JPanel(new GridLayout(1, 1));
        String[] tests = { "A+ Certification", "Network+ Certification",
                "Security+ Certification", "CIT Full Test Package" };
        JComboBox packageChoice = new JComboBox(tests);
        pane2.add(packageChoice);

        masterPanel.add(pane1);
        masterPanel.add(pane2);

        frame.add(masterPanel);
        frame.pack();

        frame.setVisible(true);
//      frame.setSize(250, 250);

    }

    public JMenuBar buildMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        return menuBar;
    }
}

我将 JMenuBar 代码移至其自己的方法中,因此希望代码的其余部分更容易理解。

我有一个主 JPanel,所有其他组件都添加到其中。

我使用另一个 JPanel 来保存两个 JLabel 和 JButton。

我使用第三个 JPanel 来容纳 JComboBox。

基本模式如下:

  • 定义 JPanel。
  • 定义组件。
  • 将组件添加到 JPanel。
  • 将 JPanel 添加到主 JPanel
  • 将主 JPanel 添加到 JFrame。

Components in Swing have to be laid out in a certain order.

You start with a JFrame. The only components that are placed in the JFrame are a JMenuBar and a JPanel. You do not add any other components in a JFrame. You add components in a JPanel.

Here's Nick's code, reorganized to define the components in the correct order. I used GridLayout because it was quicker. You should use GridBagLayout, as Nivas said.

public class Bar {

    private static final long serialVersionUID = 1L;

    public Bar(final JFrame frame) {
        JMenuBar menuBar = buildMenuBar();
        frame.setJMenuBar(menuBar);

        JPanel masterPanel = new JPanel(new GridLayout(2, 1));

        JPanel pane1 = new JPanel(new GridLayout(3, 1));
        pane1.add(new JLabel("Welcome to the CIT Test Program "));
        pane1.add(new JLabel("Please select which Test Package from the list below."));
        JButton button = new JButton("Click Me");
        pane1.add(button);

        JPanel pane2 = new JPanel(new GridLayout(1, 1));
        String[] tests = { "A+ Certification", "Network+ Certification",
                "Security+ Certification", "CIT Full Test Package" };
        JComboBox packageChoice = new JComboBox(tests);
        pane2.add(packageChoice);

        masterPanel.add(pane1);
        masterPanel.add(pane2);

        frame.add(masterPanel);
        frame.pack();

        frame.setVisible(true);
//      frame.setSize(250, 250);

    }

    public JMenuBar buildMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        return menuBar;
    }
}

I moved the JMenuBar code into its own method so, hopefully, the rest of the code is easier to understand.

I have a master JPanel, that all of the other components are added to.

I used another JPanel to hold the two JLabels and the JButton.

I used a third JPanel to hold the JComboBox.

The basic pattern is as follows:

  • Define the JPanel.
  • Define the components.
  • Add the components to the JPanel.
  • Add the JPanel to the master JPanel
  • Add the master JPanel to the JFrame.
海螺姑娘 2024-09-12 21:41:42

缺口。我觉得你首先要明确的一点就是清楚地知道自己想要什么。

我在这个问题上帮助了你 https://stackoverflow.com/questions/3055777/how-to-...

根据您的描述,您已经拥有所需的内容。这是怎么回事:

替代文字

所以,我真的认为你应该清楚地描述你想要什么,才能得到它。

我们会帮助您,但您必须清楚地定义您的问题是什么。

Nick. I think the first thing you have to clear out is to know exactly what you want.

I helped you in this question https://stackoverflow.com/questions/3055777/how-to-...

From what you're describing you already have what you need. What's wrong with this:

alt text

So, I really think you should clearly describe what you want, in order to get it.

We would help you, but you have to clearly define what your problem is.

寒江雪… 2024-09-12 21:41:42

1.你的第二个程序无法编译。您正在尝试将 JFrame 分配给 JPanel。

应该是

 JFrame frame = new Bar(new JFrame("CIT Test Program")); 

2 。有用。你想要的就在那里。但不是你想要的那样,因为你还没有告诉java你希望它如何显示。尝试使用 LayoutManager 就像 GridBagLayout

如果您急于创建 GUI,请尝试像 NetBeans 这样的 IDE,它可以通过拖放使您的工作变得更轻松。

编辑:

示例:

public class TestN extends JFrame
{
    private JLabel label ;
    private JComboBox combo;
    private JButton button;
    public TestN()
    {
        label = new JLabel("Label:");
        combo = new JComboBox();
        combo.addItem("Item 1");
        combo.addItem("Item 2");
        combo.addItem("Item 3");

        setLayout(new GridBagLayout());

        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridx = 0;
        c1.gridy = 0;
        c1.weightx = 1;
        c1.weighty = 1;
        add(label, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 1;
        c2.gridy = 0;
        c2.weightx = 1;
        c2.weighty = 1;
        add(combo, c2);

        button = new JButton("Ok");
        GridBagConstraints c3 = new GridBagConstraints();
        c3.gridx = 2;
        c3.gridy = 0;
        c3.weightx = 1;
        c3.weighty = 1;
        add(button, c3);

        setTitle("Test");
        setSize(200,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new TestN();
    }
}

免责声明:这是一个基本示例,只是为了举例。并不意味着是生产代码;-)

1 . Your second program wont compile. You are trying to assign a JFrame to a JPanel.

It should have been

 JFrame frame = new Bar(new JFrame("CIT Test Program")); 

2 . It works. What you want is there. But not as you want it, because you have not told java how you want it to display. Try using a LayoutManager like GridBagLayout.

If you are in a hurry to create a GUI, try a IDE like NetBeans that makes your job easier via drag and drop.

EDIT:

An Example:

public class TestN extends JFrame
{
    private JLabel label ;
    private JComboBox combo;
    private JButton button;
    public TestN()
    {
        label = new JLabel("Label:");
        combo = new JComboBox();
        combo.addItem("Item 1");
        combo.addItem("Item 2");
        combo.addItem("Item 3");

        setLayout(new GridBagLayout());

        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridx = 0;
        c1.gridy = 0;
        c1.weightx = 1;
        c1.weighty = 1;
        add(label, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 1;
        c2.gridy = 0;
        c2.weightx = 1;
        c2.weighty = 1;
        add(combo, c2);

        button = new JButton("Ok");
        GridBagConstraints c3 = new GridBagConstraints();
        c3.gridx = 2;
        c3.gridy = 0;
        c3.weightx = 1;
        c3.weighty = 1;
        add(button, c3);

        setTitle("Test");
        setSize(200,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new TestN();
    }
}

Disclaimer: This is a basic example just to give an example. Not meant to be production code ;-)

把人绕傻吧 2024-09-12 21:41:42

您有:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);

每个 Bar 对象都继承自 JFrame(即 Bar 对象一种 JFrame),但您在构造函数中传入了不同的 JFrame 对象(frame 参数)。然后,您可以调用 Bar 对象上的一些方法 - 例如添加两个 JLabel 对象 - 以及传入的 JFrame 上的一些方法 - 例如setVisiblesetSize 方法。

因此,您有两个不同的 JFrame 对象,您对每个对象做了一些工作,但只有其中一个被设置为可见。

看起来您将 JFrame 传递给构造函数的唯一原因是设置窗口的标题。如果将带有标题的 String 传递给 Bar 构造函数,然后调用 super(title); 作为 Bar 的第一行>Bar 构造函数,那么您根本不需要传入的 JFrame

You have:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);

Each Bar object inherits from JFrame (i.e. a Bar object is a type of JFrame) but you pass in a different JFrame object (the frame parameter) in the constructor. You then call some methods on the Bar object - such as adding the two JLabel objects - and some on the passed-in JFrame - such as the setVisible and setSize methods.

So you have two different JFrame objects and you do some work to each of them and only one of them is ever set to be visible.

It looks as is the only reason you are passing in the JFrame to the constructor is to set the title of the window. If you pass in a String with the title to the Bar constructor and then call super(title); as the first line of the Bar constructor then you won't need the passed-in JFrame at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文