java - 按钮放置和位置不起作用

标签 java swing jbutton layout-manager null-layout-manager

我最近尝试了 Swing 和 Java GUI 的在线教程,我决定复制教程代码,但添加第三个按钮。我尝试添加一个红色的,并且成功地做到了(它还没有做任何事情),但是我遇到了一些坐标问题。每当我尝试运行它时,就会出现这样的情况:

enter image description here

我真正需要的帮助是坐标或框架的大小。我真的不知道从这里该去哪里。我确信这是一个相对简单的问题,但是谢谢!

import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Test1 implements  ActionListener
{

// Definition of global values and items that are part of the GUI.
int blackScoreAmount = 0;
int greenScoreAmount = 0;
int redScoreAmount = 0;

JPanel titlePanel, scorePanel, buttonPanel;
JLabel blackLabel, greenLabel, redLabel, blackScore, greenScore, redScore;
JButton blackButton, greenButton, redButton, resetButton;

public JPanel createContentPane ()
{

    // We create a bottom JPanel to place everything on.
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    // Creation of a Panel to contain the title labels
    titlePanel = new JPanel();
    titlePanel.setLayout(null);
    titlePanel.setLocation(10, 0);
    titlePanel.setSize(180, 30);
    totalGUI.add(titlePanel);

    blackLabel = new JLabel("Black Team");
    blackLabel.setLocation(0, 0);
    blackLabel.setSize(120, 30);
    blackLabel.setHorizontalAlignment(0);
    blackLabel.setForeground(Color.black);
    titlePanel.add(blackLabel);

    greenLabel = new JLabel("Green Team");
    greenLabel.setLocation(130, 0);
    greenLabel.setSize(120, 30);
    greenLabel.setHorizontalAlignment(0);
    greenLabel.setForeground(Color.green);
    titlePanel.add(greenLabel);

    redLabel = new JLabel("Red Team");
    redLabel.setLocation(250, 0);
    redLabel.setSize(120, 30);
    redLabel.setHorizontalAlignment(0);
    redLabel.setForeground(Color.red);
    titlePanel.add(redLabel);

    // Creation of a Panel to contain the score labels.
    scorePanel = new JPanel();
    scorePanel.setLayout(null);
    scorePanel.setLocation(10, 40);
    scorePanel.setSize(180, 30);
    totalGUI.add(scorePanel);

    blackScore = new JLabel(""+blackScoreAmount);
    blackScore.setLocation(0, 0);
    blackScore.setSize(120, 30);
    blackScore.setHorizontalAlignment(0);
    scorePanel.add(blackScore);

    greenScore = new JLabel(""+greenScoreAmount);
    greenScore.setLocation(130, 0);
    greenScore.setSize(120, 30);
    greenScore.setHorizontalAlignment(0);
    scorePanel.add(greenScore);

    redScore = new JLabel(""+redScoreAmount);
    redScore.setLocation(250, 0);
    redScore.setSize(120, 30);
    redScore.setHorizontalAlignment(0);
    scorePanel.add(redScore);

    // Creation of a Panel to contain all the JButtons.
    buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setLocation(10, 80);
    buttonPanel.setSize(380, 80);
    totalGUI.add(buttonPanel);

    // We create a button and manipulate it using the syntax we have
    // used before. Now each button has an ActionListener which posts 
    // its action out when the button is pressed.
    blackButton = new JButton("Black Score");
    blackButton.setLocation(0, 0);
    blackButton.setSize(120, 30);
    blackButton.addActionListener(this);
    buttonPanel.add(blackButton);

    greenButton = new JButton("Green Score");
    greenButton.setLocation(130, 0);
    greenButton.setSize(120, 30);
    greenButton.addActionListener(this);
    buttonPanel.add(greenButton);

    redButton = new JButton("Red Score");
    redButton.setLocation(250, 0);
    redButton.setSize(120, 30);
    redButton.addActionListener(this);
    buttonPanel.add(redButton);

    resetButton = new JButton("Reset Score");
    resetButton.setLocation(0, 40);
    resetButton.setSize(250, 30);
    resetButton.addActionListener(this);
    buttonPanel.add(resetButton);

    totalGUI.setOpaque(true);
    return totalGUI;
}

// This is the new ActionPerformed Method.
// It catches any events with an ActionListener attached.
// Using an if statement, we can determine which button was pressed
// and change the appropriate values in our GUI.
public void actionPerformed(ActionEvent e) 
{
    if(e.getSource() == blackButton)
    {
        blackScoreAmount = blackScoreAmount + 1;
        blackScore.setText(""+blackScoreAmount);
    }
    else if(e.getSource() == greenButton)
    {
        greenScoreAmount = greenScoreAmount + 1;
        greenScore.setText(""+greenScoreAmount);
    }
    else if(e.getSource() == resetButton)
    {
        blackScoreAmount = 0;
        greenScoreAmount = 0;
        blackScore.setText(""+blackScoreAmount);
        greenScore.setText(""+greenScoreAmount);
    }
}

private static void createAndShowGUI() 
{

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Black and Green");

    //Create and set up the content pane.
    Test1 demo = new Test1();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(280, 190);
    frame.setVisible(true);
}

public static void main(String[] args) 
{
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run() 
        {
            createAndShowGUI();
        }
    });
  }
}  

最佳答案

您的框架是 280x190:

frame.setSize(280, 190);

并且您尝试放置一个 x 原点为 250、宽度大小为 120 -> 370 的红色按钮。如果添加新元素,只需相应调整窗口框架大小即可。

对于此类任务,使用布局可能会更好(至少更简单)。

关于java - 按钮放置和位置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105757/

相关文章:

java - HashMap 和 float[] 上的 Hadoop 序列化

java - 保留对 Locale 实例的静态引用好吗?

java - 在 Tomcat 上部署 .war 时图像路径不正确

java - JPanel 不更新,无论我尝试什么

java - 无法在 JTable 的单个单元格中添加两个按钮

java - Spring MVC SseEmitter - 连接异常中止

java - Guice 3.0 - 对于仅具有依赖注入(inject)的 Swing 应用程序,我需要哪些 jar?

java - 跳过一行 GridBagLayout

Java Swing JButton 未显示在 JPanel 上

java - 如何根据用户输入生成JButton?