如何在同一个JFrame上同时显示多个按钮?

huangapple go评论75阅读模式
英文:

How to Display Multiple Buttons Simultaneously on same JFrame?

问题

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

public class SpeedMathGoStartScreen extends JFrame{

    JLabel background;
    JLabel title;
    JButton play;
    JPanel playPanel;
    JButton guide;
    JPanel guidePanel;

    public SpeedMathGoStartScreen() {

        title = new JLabel("SPEED MATH GO");
        title.setFont(new Font("Comic Sans", Font.BOLD, 96));
        title.setBounds(90, 0, 900, 100);
        this.add(title);

        ImageIcon backgroundImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\ttt.png");
        background = new JLabel(backgroundImage);
        background.setBounds(0, 0, 1000, 700);
        this.add(background);

        ImageIcon playImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_play-circle_2561292.png");
        play = new JButton("Play", playImage);
        playPanel = new JPanel();
        playPanel.setLayout(null);
        playPanel.setBounds(345, 200, 300, 150);
        play.setBounds(345, 200, 300, 150);
        play.setFont(new Font("Comic Sans", Font.BOLD, 48));
        play.setBackground(Color.RED);
        play.setVisible(true);
        playPanel.add(play);
        this.add(playPanel);

        ImageIcon guideImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_thefreeforty_map_1243687.png");
        guide = new JButton("Guide", guideImage);
        guidePanel = new JPanel();
        guidePanel.setLayout(null);
        guidePanel.setBounds(320, 400, 350, 150);
        guide.setBounds(320, 400, 350,  150);
        guide.setFont(new Font("Comic Sans", Font.BOLD, 48));
        guide.setBackground(Color.CYAN);
        guide.setVisible(true);
        guidePanel.add(guide);
        this.add(guidePanel);

        play.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                    new SpeedMathGoGUI();
                    dispose();
                }
            });

        guide.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        
                        new SpeedMathGoGuide();
                        dispose();
                    }
                }
                );
        
        // Adjustments: Instead of using null layout, use layout managers
        setLayout(new FlowLayout());  // Use a layout manager for the JFrame
        // Add buttons directly to the JFrame, not separate JPanels
        add(play);
        add(guide);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000, 700);
        setVisible(true);
    }
}

Note: The adjustments made in the code involve using layout managers (FlowLayout) to properly position and display the buttons directly on the JFrame, rather than using null layouts and separate JPanel containers. This should ensure that both buttons are displayed correctly.

英文:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
public class SpeedMathGoStartScreen extends JFrame{
JLabel background;
JLabel title;
JButton play;
JPanel playPanel;
JLabel playLabel;
JButton guide;
JPanel guidePanel;
public SpeedMathGoStartScreen() {
title = new JLabel("SPEED MATH GO");
title.setFont(new Font("Comic Sans", Font.BOLD, 96));
title.setBounds(90, 0, 900, 100);
this.add(title);
ImageIcon backgroundImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\ttt.png");
background = new JLabel(backgroundImage);
background.setBounds(0, 0, 1000, 700);
this.add(background);
ImageIcon playImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_play-circle_2561292.png");
play = new JButton("Play", playImage);
playPanel = new JPanel();
playPanel.setLayout(null);
playPanel.setBounds(345, 200, 300, 150);
play.setBounds(345, 200, 300, 150);
play.setFont(new Font("Comic Sans", Font.BOLD, 48));
play.setBackground(Color.RED);
play.setVisible(true);
playPanel.add(play);
this.add(playPanel);
ImageIcon guideImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_thefreeforty_map_1243687.png");
guide = new JButton("Guide", guideImage);
guidePanel = new JPanel();
guidePanel.setLayout(null);
guidePanel.setBounds(320, 400, 350, 150);
guide.setBounds(320, 400, 350,  150);
guide.setFont(new Font("Comic Sans", Font.BOLD, 48));
guide.setBackground(Color.CYAN);
guide.setVisible(true);
guidePanel.add(guide);
this.add(guidePanel);
//play.addActionListener(new ActionListener()
//   {
//   public void actionPerformed(ActionEvent e)
//    {
// 	  new SpeedMathGoGUI();
//    }
//   });
play.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SpeedMathGoGUI();
dispose();
}
});
guide.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SpeedMathGoGuide();
dispose();
}
}
);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 700);
setVisible(true);
}
}

If you run this on Eclipse, only the guide button shows. When I comment out line 56 (should be this.add(guidePanel)), only the play button shows. What adjustments I must make to my code in order for both to display on the JFrame? I would also like to know if other classes may have affected this error as this transitions to two others when the buttons are pressed.

答案1

得分: 1

不要使用空布局!不要使用setBounds()!

Swing被设计用于与布局管理器一起使用,因此您需要了解布局管理器的工作原理。

> 仅按钮指示显示。

this.add(playPanel);
...
this.add(guidePanel);

默认情况下,JFrame的内容窗格使用BorderLayout

在上面的代码中,当您将组件添加到BorderLayout并且不指定约束时,将使用CENTER约束。然而,“CENTER”只能显示一个组件,因此您会看到最后添加的那个。

> 如何同时显示多个按钮

如果您想在面板上放置两个按钮(或任何组件),请将两个按钮添加到面板上。基本代码如下:

JPanel buttonsPanel = new JPanel();
buttonsPanel.add(play);
buttonsPanel.add(guide);
this.add(buttonsPanel);

JPanel的默认布局管理器是FlowLayout。这是一种简单的布局。组件显示在同一行上,添加组件时不需要约束。

阅读Swing教程中有关布局管理器的部分,了解更多信息并查看其他布局管理器的工作示例。您可以根据需求选择适当的布局管理器。您始终可以使用不同的布局管理器嵌套面板,以实现所需的布局。

英文:

Don't use a null layout!!! Don't use setBounds()!!!

Swing was designed to be used with layout managers, so you need to understand how layout managers work.

> only the guide button shows.

this.add(playPanel);
...
this.add(guidePanel);

By default the content pane of a JFrame uses a BorderLayout.

In your code above, when you add a component to a BorderLayout and don't specify a constraint, then the CENTER constraint is used. However, only one component can be displayed in the "CENTER" so you see the last one added.

> How to Display Multiple Buttons Simultaneously

If you want two buttons (or any component) on a panel then add two buttons to a panel. The basic code would be:

JPanel buttonsPanel = new JPanel();
buttonsPanel.add( play );
buttonsPanel.add( guide );
this.add(buttonsPanel);

The default layout manager for a JPanel is the FlowLayout. It is a simple layout. The components are displayed on the same line and no constraint is needed when the component is added.

Read the section from the Swing tutorial on Layout Managers for more information and working examples of other layout managers. You choose the appropriate layout manager for your requirement. You can always nest panels using different layout managers to achieve your desired layout.

huangapple
  • 本文由 发表于 2020年10月20日 23:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定