Java 按钮位置和位置不工作
我最近试用了Swing和JavaGUI的在线教程,我决定复制教程代码,但添加了第三个按钮。我尝试添加一个红色的,我成功地添加了(它还没有做任何事情),但是我有一些协调问题。每当我尝试运行它时,都会出现如下情况: 我真正需要帮助的是坐标或帧的大小。我真的不知道该怎么办。我相信这是一个相对简单的问题,但是,谢谢Java 按钮位置和位置不工作,java,swing,jbutton,layout-manager,null-layout-manager,Java,Swing,Jbutton,Layout Manager,Null Layout Manager,我最近试用了Swing和JavaGUI的在线教程,我决定复制教程代码,但添加了第三个按钮。我尝试添加一个红色的,我成功地添加了(它还没有做任何事情),但是我有一些协调问题。每当我尝试运行它时,都会出现如下情况: 我真正需要帮助的是坐标或帧的大小。我真的不知道该怎么办。我相信这是一个相对简单的问题,但是,谢谢 import javax.swing.*; import java.awt.Color; import java.awt.event.ActionListener; import java
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。如果添加新元素,只需相应调整窗框大小即可
对于这类任务,使用布局可能更好(至少更简单)。窗口的大小是“硬编码的”: 按钮的大小和位置也是如此:
redButton.setLocation(250, 0);
redButton.setSize(120, 30);
您将一个宽度为120的按钮放在一个280大的框架的250处,这样只有25%的按钮可以看到
您必须调整框架的大小和按钮的位置,使其完全可见且美观
编辑
现在,许多其他回复者建议,如果您想改进您的应用程序,请尝试摆脱所有硬编码值,改用布局管理器
以下是一些很好的解释:
.setLayout(null)代码>废话和使用布局管理器。永远不要手动调整组件大小/定位。相反,使用合适的LayoutManager@kleopatra谢谢,现在我明白为什么了!在错误的方向上遇到问题是学习的一个很好的动力:-)技术上是正确的,尽管没有解决真正的问题(即不使用布局管理器),我只是试图通过尽可能保持代码的方式来回答这个问题。我知道这不是最好的方法,但如果OP遵循这种教程,他可能不知道什么是LayoutManager,他可能会在以后学习。任何没有LayoutManager的教程都应该扔进垃圾桶;-)顺便说一句,这不是我的反对票——但你可能可以通过添加一行建议使用经理来恢复我的反对票。我完全同意你;)我会这么做的,这样做可能会更有效率,而不是把答案胡乱扔给新手。
redButton.setLocation(250, 0);
redButton.setSize(120, 30);