JFrame 无法正常工作
这让我非常生气,我已经为此工作了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Swing 中的组件必须按一定的顺序排列。
您从 JFrame 开始。放置在 JFrame 中的唯一组件是 JMenuBar 和 JPanel。您无需在 JFrame 中添加任何其他组件。您在 JPanel 中添加组件。
这是 Nick 的代码,经过重新组织,以正确的顺序定义组件。我使用 GridLayout 因为它更快。正如 Nivas 所说,您应该使用 GridBagLayout。
我将 JMenuBar 代码移至其自己的方法中,因此希望代码的其余部分更容易理解。
我有一个主 JPanel,所有其他组件都添加到其中。
我使用另一个 JPanel 来保存两个 JLabel 和 JButton。
我使用第三个 JPanel 来容纳 JComboBox。
基本模式如下:
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.
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:
缺口。我觉得你首先要明确的一点就是清楚地知道自己想要什么。
我在这个问题上帮助了你 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:
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.
1.你的第二个程序无法编译。您正在尝试将 JFrame 分配给 JPanel。
应该是
2 。有用。你想要的就在那里。但不是你想要的那样,因为你还没有告诉java你希望它如何显示。尝试使用 LayoutManager 就像 GridBagLayout。
如果您急于创建 GUI,请尝试像 NetBeans 这样的 IDE,它可以通过拖放使您的工作变得更轻松。
编辑:
示例:
免责声明:这是一个基本示例,只是为了举例。并不意味着是生产代码;-)
1 . Your second program wont compile. You are trying to assign a JFrame to a JPanel.
It should have been
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:
Disclaimer: This is a basic example just to give an example. Not meant to be production code ;-)
您有:
每个
Bar
对象都继承自JFrame
(即Bar
对象是一种JFrame
),但您在构造函数中传入了不同的JFrame
对象(frame
参数)。然后,您可以调用Bar
对象上的一些方法 - 例如添加两个JLabel
对象 - 以及传入的JFrame
上的一些方法 - 例如setVisible
和setSize
方法。因此,您有两个不同的 JFrame 对象,您对每个对象做了一些工作,但只有其中一个被设置为可见。
看起来您将 JFrame 传递给构造函数的唯一原因是设置窗口的标题。如果将带有标题的
String
传递给Bar
构造函数,然后调用super(title);
作为Bar
的第一行>Bar 构造函数,那么您根本不需要传入的JFrame
You have:
Each
Bar
object inherits fromJFrame
(i.e. aBar
object is a type ofJFrame
) but you pass in a differentJFrame
object (theframe
parameter) in the constructor. You then call some methods on theBar
object - such as adding the twoJLabel
objects - and some on the passed-inJFrame
- such as thesetVisible
andsetSize
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 aString
with the title to theBar
constructor and then callsuper(title);
as the first line of theBar
constructor then you won't need the passed-inJFrame
at all