我试图让两个按钮显示,彼此之间有一点点空间。如果我尝试使用BorderLayout,则防御按钮会占据整个屏幕。我已经尝试了所有其他布局,但没有运气。我正在尝试打印出JLabel,但根据互联网,你必须添加一个布局才能显示它。无论如何都要这样做,同时保持布局为null?
以下是代码:
package jframe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class klose extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
JButton sword = new JButton("Sword");
JButton axe = new JButton("Axe");
JLabel bob = new JLabel("Choose a weapon...");
JFrame frame = new JFrame("Combat Demo");
frame.setSize(600, 600);
frame.setVisible(true);
JButton attack = new JButton("Attack!");
JButton defend = new JButton("Defend!");
frame.setLayout(null);
attack.setSize(100, 50);
defend.setSize(100, 50);
attack.setLocation(400, 400);
defend.setLocation(100, 400);
frame.add(attack);
frame.add(defend);
attack.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
attack.setVisible(false);
defend.setVisible(false);
bob.setText("Choose a weapon...");
bob.setLocation(0, 0);
bob.setVisible(true);
sword.setSize(100, 50);
axe.setSize(100, 50);
sword.setLocation(400, 400);
axe.setLocation(100, 400);
sword.setVisible(true);
axe.setVisible(true);
frame.add(sword);
frame.add(axe);
frame.add(bob);
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您可以通过调用此方法setLayout(null)
来执行null布局,但是您需要从Dimension
和Insets
获取值以用作setBounds(x, y, width, height)
方法的参数。
您的代码在我的末尾编译和执行。我必须对您的代码进行一些功能和可读性方面的改进。摘录如下。
package app;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
//Please follow standard naming convention for java classes
public class GUIApp extends JFrame {
private static final long serialVersionUID = 1L;
private static JButton attack = new JButton("Attack!");
private static JButton axe = new JButton("Axe");
private static JLabel bob = new JLabel("Choose a weapon...");
private static JButton defend = new JButton("Defend!");
private static Insets insets = null;
private static Dimension size = null;
private static JButton sword = new JButton("Sword");
public GUIApp() {
//Notice the layout is null
setLayout(null);
//Insets need to be referenced
insets = getContentPane().getInsets();
//Layout stuffs
setTitle("Combat Demo");
setSize(600,600);
attack.setSize(100, 50);
axe.setSize(100, 50);
defend.setSize(100, 50);
sword.setSize(100, 50);
//Toggle visibility
attack.setVisible(true);
defend.setVisible(true);
sword.setVisible(false);
axe.setVisible(false);
bob.setVisible(false);
//Add components
add(attack);
add(defend);
add(sword);
add(axe);
add(bob);
}
public static void main(String[] args) {
//Instantiate the object
GUIApp app = new GUIApp();
app.setVisible(true);
app.setDefaultCloseOperation(EXIT_ON_CLOSE);
//You could obtain dimension and set bounds in the main method, or anywhere else.
size = attack.getPreferredSize();
attack.setBounds(400 + insets.left, 400 + insets.top, size.width, size.height);
size = defend.getPreferredSize();
defend.setBounds(100 + insets.left, 400 + insets.top, size.width, size.height);
//Action listener for attack button
attack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
attack.setVisible(false);
defend.setVisible(false);
bob.setVisible(true);
sword.setVisible(true);
axe.setVisible(true);
//You could obtain dimension and set bounds in the actionPerformed method.
size = bob.getPreferredSize();
bob.setBounds(0 + insets.left, 0 + insets.top, size.width, size.height);
size = sword.getPreferredSize();
sword.setBounds(400 + insets.left, 400 + insets.top, size.width, size.height);
size = axe.getPreferredSize();
axe.setBounds(100 + insets.left, 400 + insets.top, size.width, size.height);
}
});
}
}