Java 使所有按钮大小相同
请看下面的代码Java 使所有按钮大小相同,java,swing,Java,Swing,请看下面的代码 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class GUI extends JFrame { private JButton open, process; private JLabel center; private JScrollPane scroll; private Box box; private IplImage image
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUI extends JFrame
{
private JButton open, process;
private JLabel center;
private JScrollPane scroll;
private Box box;
private IplImage image;
public FirstGUI()
{
open = new JButton("Open Image");
open.setPreferredSize(new Dimension(70,20));
open.setMaximumSize(new Dimension(100,20));
open.addActionListener(new OpenImageAction());
process = new JButton("Process");
process.setPreferredSize(new Dimension(100,20));
process.setMinimumSize(new Dimension(100,20));
process.addActionListener(new ProcessAction());
System.out.println("Open Size: "+open.getSize()+" Process size: "+process.getSize());
box = new Box(BoxLayout.Y_AXIS);
box.add(open);
box.add(process);
center = new JLabel();
scroll = new JScrollPane(center);
getContentPane().add(box,"West");
getContentPane().add(scroll,"Center");
this.setSize(300,300);
this.pack();
this.validate();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GUI();
}
catch(java.lang.Exception e)
{
JOptionPane.showMessageDialog(null,"GUI Error");
}
}
我想把所有的钮扣都做成同样的尺寸。在这里,第一个比第二个宽。我需要两者的宽度和高度相同。如您所见,我已经使用了所有可用功能,如setPreferedSize()、setMaximumSize()、setMinimumSize(),但它仍然不能正常工作。请帮忙 可能是因为
setPreferredSize()的维度不同。
下面是使用
GridLayout
实现这一点的一种方法。我还引入了一个额外的JPanel
,这样当调整JFrame
的大小时按钮不会被拉伸,我为它选择了gridbagloayout
,这样它将垂直居中按钮面板。肯定还有其他方法可以解决你的问题
您应该避免的一件事是强制使用首选/最大/最小尺寸。将此委托给L&F和布局管理员
如果在JFrame
上调用pack()
,则之前设置其大小是无用的,因为pack()
无论如何都会更改它。尝试调用setVisible(true)代码>GUI初始化的最后一行
如果您想正确理解布局、定位、大小调整等。。。作品在摇摆中,我强烈推荐阅读
为两个按钮设置相同的尺寸
设置两个按钮的最大和最小大小。现在只有第一个最大值,第二个最小值。
会有用的
您可以创建自己的布局管理器,用于计算所有按钮的最大大小(可能来自首选大小值),并基本上分配相同的大小(通过setSize)。当然,您还必须提供位置信息,但这提供了提供垂直和水平布局选项的优势
更新
这是我使用的实现,我不相信它,因为你可以看到它是由Santhosh Kumar编写的。我发现它非常有用,因为它不仅将所有按钮的大小调整为相同的大小,而且还提供对齐。你可以在这里找到原来的帖子
/**
*@作者Santhosh Kumar-santhosh@in.fiorano.com
*/
公共类ButtonLayout实现布局管理器、SwingConstants{
受保护的静态记录器=Logger.getLogger(ButtonLayout.class);
私人内部差距;
私有int对齐;
公共按钮布局(){
设置对齐(右);
setGap(2);
}
公共按钮布局(内部对齐,内部间隙){
setGap(gap);
设置对齐(对齐);
}
公共按钮布局(内部间隙){
这个(右,缺口),;
}
公共int getAlignment(){
返回对齐;
}
公共void集合对齐(int对齐){
这个。对齐=对齐;
}
公共int getGap(){
返回间隙;
}
公共空间设置间距(内部间距){
这个.gap=gap;
}
专用维度[]维度(组件子项[]){
int maxWidth=0;
int maxHeight=0;
int visibleCount=0;
尺寸组件首选尺寸;
for(int i=0,c=children.length;i open.setPreferredSize(new Dimension(70,20));
process.setPreferredSize(new Dimension(100,20));
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class GUI extends JFrame {
private JButton open, process;
private JLabel center;
private JScrollPane scroll;
private JPanel box;
public GUI() {
open = new JButton("Open Image");
// open.addActionListener(new OpenImageAction());
process = new JButton("Process");
// process.addActionListener(new ProcessAction());
box = new JPanel(new GridLayout(2, 1));
box.add(open);
box.add(process);
JPanel west = new JPanel(new GridBagLayout());
west.add(box);
center = new JLabel("Some center label");
scroll = new JScrollPane(center);
getContentPane().add(west, BorderLayout.WEST);
getContentPane().add(scroll);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (java.lang.Exception e) {
JOptionPane.showMessageDialog(null, "GUI Error");
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI();
}
});
}
}
/**
* @author Santhosh Kumar - santhosh@in.fiorano.com
*/
public class ButtonLayout implements LayoutManager, SwingConstants {
protected static Logger logger = Logger.getLogger(ButtonLayout.class);
private int gap;
private int alignment;
public ButtonLayout() {
setAlignment(RIGHT);
setGap(2);
}
public ButtonLayout(int alignment, int gap) {
setGap(gap);
setAlignment(alignment);
}
public ButtonLayout(int gap) {
this(RIGHT, gap);
}
public int getAlignment() {
return alignment;
}
public void setAlignment(int alignment) {
this.alignment = alignment;
}
public int getGap() {
return gap;
}
public void setGap(int gap) {
this.gap = gap;
}
private Dimension[] dimensions(Component children[]) {
int maxWidth = 0;
int maxHeight = 0;
int visibleCount = 0;
Dimension componentPreferredSize;
for (int i = 0, c = children.length; i < c; i++) {
if (children[i].isVisible()) {
componentPreferredSize = children[i].getPreferredSize();
maxWidth = Math.max(maxWidth, componentPreferredSize.width);
maxHeight = Math.max(maxHeight, componentPreferredSize.height);
visibleCount++;
}
}
int usedWidth = 0;
int usedHeight = 0;
switch (alignment) {
case LEFT:
case RIGHT:
usedWidth = maxWidth * visibleCount + gap * (visibleCount - 1);
usedHeight = maxHeight;
break;
case TOP:
case BOTTOM:
usedWidth = maxWidth;
usedHeight = maxHeight * visibleCount + gap * (visibleCount - 1);
break;
}
return new Dimension[]{
new Dimension(maxWidth, maxHeight),
new Dimension(usedWidth, usedHeight),};
}
public void layoutContainer(Container container) {
Insets insets = container.getInsets();
int width = container.getWidth() - (insets.left + insets.right);
int height = container.getHeight() - (insets.top + insets.bottom);
Component[] children = container.getComponents();
Dimension dim[] = dimensions(children);
int maxWidth = dim[0].width;
int maxHeight = dim[0].height;
int usedWidth = dim[1].width;
int usedHeight = dim[1].height;
for (int i = 0, c = children.length; i < c; i++) {
if (children[i].isVisible()) {
switch (alignment) {
case LEFT:
children[i].setBounds(
insets.left + (maxWidth + gap) * i,
insets.top,
maxWidth,
maxHeight);
break;
case TOP:
children[i].setBounds(
insets.left + ((width - maxWidth) / 2),
insets.top + (maxHeight + gap) * i,
maxWidth,
maxHeight);
break;
case RIGHT:
children[i].setBounds(
width - insets.right - usedWidth + (maxWidth + gap) * i,
insets.top,
maxWidth,
maxHeight);
break;
case BOTTOM:
children[i].setBounds(
insets.left + (maxWidth + gap) * i,
height - insets.bottom - usedHeight + (maxHeight + gap) * i,
// insets.top,
maxWidth,
maxHeight);
break;
}
}
}
}
public Dimension minimumLayoutSize(Container c) {
return preferredLayoutSize(c);
}
public Dimension preferredLayoutSize(Container container) {
Insets insets = container.getInsets();
Component[] children = container.getComponents();
Dimension dim[] = dimensions(children);
int usedWidth = dim[1].width;
int usedHeight = dim[1].height;
return new Dimension(
insets.left + usedWidth + insets.right,
insets.top + usedHeight + insets.bottom);
}
public void addLayoutComponent(String string, Component comp) {
}
public void removeLayoutComponent(Component c) {
}
}