按钮位置和位置不起作用

时间:2013-04-19 13:27:43

标签: 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();
        }
    });
  }
}  

2 个答案:

答案 0 :(得分:1)

你的画面是280x190:

frame.setSize(280, 190);

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

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

答案 1 :(得分:0)

您的窗口大小是“硬编码”:

frame.setSize(280, 190);

按钮的大小和位置也是如此:

redButton.setLocation(250, 0);
redButton.setSize(120, 30);

您将一个宽度为120的按钮放在一个280大的框架250的位置,这样只有25%的按钮可以看到。

您必须调整框架的大小和按钮的位置,以使其完全可见且漂亮..

修改

现在,许多其他回复者建议,如果您想改进您的应用程序,请尝试删除所有硬编码值并改为使用布局管理器。

以下是对它们的一些很好的解释: