Java GUI - JPanel、JFrame、JButton
我正在尝试打开一个其中同时包含图像
和按钮
的窗口。但我似乎不知道如何添加按钮。图像显示良好,菜单工作正常,但无论我在何处添加按钮(添加到 JLabel
、JPanel
或 JFrame
中),它不会显示...
Main:
public static void main(String[] args) {
GUI myGUI = new GUI();
myGUI.show();
}
GUI 类: 使用菜单时调用 openImage。然后会显示图像,但没有按钮。
private JFrame myFrame;
private JPanel myPanel;
private JLabel myLabel;
public GUI()
{
myFrame = new JFrame();
initializePanel();
}
public void show()
{
myFrame.setSize(600,600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addMouseListener(this);
setupMenu(myFrame);
myFrame.setVisible(true);
}
private void initializePanel()
{
myPanel = new JPanel();
myPanel.setPreferredSize(new Dimension(500,500));
//myPanel.setLayout(new BorderLayout());
}
private void openImage(String fileName)
{
try {
myImage = ImageIO.read(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myLabel = getJLabel();
JButton button = new JButton("ButtonClick");
button.addActionListener(this);
myFrame.setContentPane(myLabel);
myPanel.add(button);
myFrame.getContentPane().add(myPanel);
myFrame.pack();
myFrame.setSize(600,600);
}
private void setupMenu(JFrame window) {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
file.add(open);
menubar.add(file);
window.setJMenuBar(menubar);
}
I'm trying to open a window that has both an image
and buttons
in it. But I can't seem to figure out how to add the button. The image displays great and the menu works fine, but no matter where I add the button (into the JLabel
, JPanel
, or JFrame
), it doesn't ever show...
Main:
public static void main(String[] args) {
GUI myGUI = new GUI();
myGUI.show();
}
GUI class: openImage is called when using the menu. The image then displays, but no button.
private JFrame myFrame;
private JPanel myPanel;
private JLabel myLabel;
public GUI()
{
myFrame = new JFrame();
initializePanel();
}
public void show()
{
myFrame.setSize(600,600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addMouseListener(this);
setupMenu(myFrame);
myFrame.setVisible(true);
}
private void initializePanel()
{
myPanel = new JPanel();
myPanel.setPreferredSize(new Dimension(500,500));
//myPanel.setLayout(new BorderLayout());
}
private void openImage(String fileName)
{
try {
myImage = ImageIO.read(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myLabel = getJLabel();
JButton button = new JButton("ButtonClick");
button.addActionListener(this);
myFrame.setContentPane(myLabel);
myPanel.add(button);
myFrame.getContentPane().add(myPanel);
myFrame.pack();
myFrame.setSize(600,600);
}
private void setupMenu(JFrame window) {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
file.add(open);
menubar.add(file);
window.setJMenuBar(menubar);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的主要问题是您将 contentPane 设置为 JLabel - 不要这样做! contentPane 需要是不透明的,需要构建为易于用作容器,并且在您的情况下,实际上应该是 JPanel。我相信 JLabel 使用空布局,因此您的代码不显示按钮也就不足为奇了。如果要显示背景图像,请从扩展 JPanel 的匿名类构造 myPanel,重写该类中的 PaintComponent 方法(在该方法中首先调用 super.paintComonent),并在此方法中绘制图像。然后,您可以将组件添加到 contentPane,它现在将使用 FlowLayout(JPanel 的默认设置),并且默认情况下它将是不透明的。
另外,如果您的目标是交换 GUI 中显示的项目,请使用 CardLayout 来为您进行交换,因为此布局使交换组件变得轻而易举。
Your main issue is your setting the contentPane to be a JLabel -- don't do this! The contentPane needs to be opaque, needs to be built to be easily used as a Container and in your case, really should be a JPanel. JLabel I believe uses a null layout so it's no surprise that your code shows no button. If you want to show a background image, make have myPanel constructed from an anonymous class that extends JPanel, override the paintComponent method in this class (calling super.paintComonent first in the method), and draw the image in this method. Then you can add components to the contentPane which will now use a FlowLayout (the default for a JPanel) and it will be opaque by default.
Also, if your goal is to swap items displayed in your GUI, use a CardLayout to do the swapping for you as this layout makes swapping components a breeze.
真的不知道,取决于如何将图片添加到 JLabel、JPanel 或 JFrame 的方法,但也许对于包含一些的简单容器,只有一两个 JComponent 存在疯狂的想法,没有副作用,有显示图片并添加 JButton 的想法:
JLabel 非常相似 JComponent 到 JPanel,默认情况下是半透明的并且非常简单的实现 Icon/ImageIcon,那么你只需调用
myLabel.setIcon(myPicture)
到所有 JComponent,您可以/可以通过使用一些 LayoutManager(框、流、GridBagLayout)
really don't know, depends of method(s) how you are added picture to the
JLabel, JPanel, or JFrame
, but maybe for simle Container that contains a few, only one-two JComponents is there crazy idea, without side effects, with idea to display picture and to add there JButton:JLabel is very similair JComponent to the JPanel, and is by default translucent and very simple implements Icon/ImageIcon, then you'll only to call
myLabel.setIcon(myPicture)
to the all of JComponents you are/could be able to add another JComponent by using some of LayoutManager (Box, Flow, GridBagLayout)
您尝试将标签设置为内容窗格,然后尝试将面板添加到该图像中,但这根本不合理。
更改它,以便将标签添加到面板并将面板作为内容窗格:
如下所示:
You tried to set the label as the content pane and then tried to add the panel to that image which doesn't make sanse at all.
Change it so you add the label to the panel and have the panel as content pane:
Like this:
你有这一行,这就是问题所在。这没有多大意义:
尝试一下:
You have this line which is the problem. It doesn't make much sense:
Try instead: