简体   繁体   English

Java GUI - JPanels,JFrames,JButtons

[英]Java GUI - JPanels, JFrames, JButtons

I'm trying to open a window that has both an image and buttons in it. 我正在尝试打开一个同时包含imagebuttons的窗口。 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... 图像显示很好,菜单工作正常,但无论我在哪里添加按钮(进入JLabelJPanelJFrame ),它都没有显示...

Main: 主要:

public static void main(String[] args) {
    GUI myGUI = new GUI();
    myGUI.show();
}

GUI class: openImage is called when using the menu. GUI类:使用菜单时调用openImage。 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);
}

Your main issue is your setting the contentPane to be a JLabel -- don't do this! 您的主要问题是您将contentPane设置为JLabel - 请勿执行此操作! 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. contentPane需要是不透明的,需要构建为容易用作Container,在你的情况下,真的应该是JPanel。 JLabel I believe uses a null layout so it's no surprise that your code shows no button. JLabel我相信使用null布局,所以你的代码没有按钮就不足为奇了。 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. 如果要显示背景图像,请使用扩展JPanel的匿名类构造myPanel,覆盖此类中的paintComponent方法(在方法中首先调用super.paintComonent),然后在此方法中绘制图像。 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. 然后,您可以将组件添加到contentPane,现在将使用FlowLayout(JPanel的默认值),默认情况下它将是不透明的。

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. 此外,如果您的目标是交换GUI中显示的项目,请使用CardLayout为您进行交换,因为此布局使交换组件变得轻而易举。

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, JPanel, or JFrame ,但也许对于包含少数的simle容器,只有一两个JComponents有疯狂的想法,没有副作用,想要显示图片并添加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) JLabel非常类似于JPanel的 JComponent ,默认是半透明且非常简单的实现Icon / ImageIcon ,那么你只需要调用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) 对于所有JComponents,您可以通过使用一些LayoutManager (Box,Flow,GridBagLayout)来添加另一个JComponent

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: 它没有多大意义:

myFrame.setContentPane(myLabel);

Try instead: 尝试改为:

myFrame.getContentPane().add(myLabel); 

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

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