gpt4 book ai didi

java - 为什么我无法在 JPanel 上调整 JButton 的大小或重新定位?

转载 作者:行者123 更新时间:2023-12-01 10:09:23 25 4
gpt4 key购买 nike

package swingtraining;

import static java.awt.Color.BLACK;
import static java.awt.Color.RED;
import java.awt.EventQueue;
import static java.awt.Font.BOLD;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;



public class JFrameWithAButton extends JFrame {

public JFrameWithAButton(){

setSize(400,400);
setTitle("Swing is hard");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

}

public static void main(String args[]){

JPanel Jp1 = new JPanel();

Jp1.setOpaque(true);

Jp1.setBackground(RED);

JButton Jbt = new JButton();

Jbt.setLayout(null);
Jbt.setSize(200,200);

Jbt.setBounds(new Rectangle(new Point(200, 200)));
Jbt.setText("Hello!");

EventQueue.invokeLater(new Runnable(){

public void run(){

JFrameWithAButton ex = new JFrameWithAButton();
ex.setVisible(true);
ex.add(Jp1);
Jp1.add(Jbt);
}
});
}

}

抱歉,如果代码有点像妈妈的意大利面条式的,但我就是无法破解这个 cookie >。> 即使将布局设置为 null,它也不会移动。关于如何让这个 JButton 不仅移动到窗口中间,而且增长 200 x 200 像素,有什么建议吗?

最佳答案

Any suggestions of how I get this JButton to not only move to the middle of the window but also grow 200 by 200 pixels?

我能想到一些,但没有一个使用 null 布局

GridBagConstraints

GridBagConstraints

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 200;
gbc.ipady = 200;

add(new JButton("Hello"), gbc);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

}

}

JButton#setMargin

JButton margins

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

JButton btn = new JButton("Hello");
btn.setMargin(new Insets(100, 100, 100, 100));
add(btn);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

}

}

空边框

EmptyBorder

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(50, 50, 50, 50));
JButton btn = new JButton("Hello");
add(btn);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

}

}

您可以组合使用它们,也许使用 EmptyBorderGridBagConstraints 来进一步约束布局。

这些示例的最大好处是,在大多数情况下,如果字体大小发生变化或字体的渲染要求发生变化,布局能够进行补偿

避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计初衷是与布局管理器一起工作,放弃它们将导致无穷无尽的问题,您将花费越来越多的时间来尝试纠正

因为读起来总是很有趣,Why is it frowned upon to use a null layout in SWING?

关于java - 为什么我无法在 JPanel 上调整 JButton 的大小或重新定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com