gpt4 book ai didi

java - 如何将已使用 borderlayout 布局的面板中的对象居中

转载 作者:行者123 更新时间:2023-11-30 10:41:04 29 4
gpt4 key购买 nike

我搜索了这个网站、其他网站和我的教科书,但找不到解决方案,所以这里是:

我有一个简单的温度。转换程序,功能很好,但我想让我的布局更具吸引力,更类似于程序中要求的内容。

现在看起来像这样:

my program output

我希望它看起来像这样:

Desired Output

不同之处在于输入和输出字段位于所需输出的中心。

这是我的程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Convert extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel convertFrom, convertTo, top, bottom;
private JLabel label3, label4;
private JTextField temperature1, temperature2;
private ButtonGroup radioFrom, radioTo;
private JRadioButton celsiusBoxTo, fahrenheitBoxTo, kelvinBoxTo, celsiusBoxFrom, fahrenheitBoxFrom, kelvinBoxFrom;


public Convert(){


fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
celsiusBoxFrom = new JRadioButton( "Celsius", false );
kelvinBoxFrom = new JRadioButton( "Kelvin", false );

radioFrom = new ButtonGroup();
radioFrom.add( fahrenheitBoxFrom );
radioFrom.add( celsiusBoxFrom );
radioFrom.add( kelvinBoxFrom );

label3 = new JLabel( "Input" );
label4 = new JLabel( "Output" );

fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
celsiusBoxTo = new JRadioButton( "Celsius", true );
kelvinBoxTo = new JRadioButton( "Kelvin", false );

radioTo = new ButtonGroup();
radioTo.add( fahrenheitBoxTo );
radioTo.add( celsiusBoxTo );
radioTo.add( kelvinBoxTo );

convertFrom = new JPanel();
convertFrom.setLayout( new GridLayout( 4, 1 ) );
convertFrom.add(new JLabel( "Input Scale" ));
convertFrom.add( fahrenheitBoxFrom );
convertFrom.add( celsiusBoxFrom );
convertFrom.add( kelvinBoxFrom );

convertTo = new JPanel();
convertTo.setLayout( new GridLayout( 4, 1 ) );
convertTo.add(new JLabel( "Output Scale" ));
convertTo.add( fahrenheitBoxTo );
convertTo.add( celsiusBoxTo );
convertTo.add( kelvinBoxTo );

temperature1 = new JTextField( 10 );
top = new JPanel();
top.setLayout(new GridLayout(1, 2));
top.add(label3);
top.add(temperature1);
temperature1.addActionListener( new ActionListener(){

public void actionPerformed( ActionEvent event ){
int convertTemp, temp;
temp = Integer.parseInt( ( ( JTextField ) event.getSource() ).getText() );

if ( fahrenheitBoxFrom.isSelected() && celsiusBoxTo.isSelected() ){
convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) );
temperature2.setText( String.valueOf( convertTemp ) );
}

else if ( fahrenheitBoxFrom.isSelected() && kelvinBoxTo.isSelected() ){
convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) + 273 );
temperature2.setText( String.valueOf( convertTemp ) );
}

else if ( celsiusBoxFrom.isSelected() && fahrenheitBoxTo.isSelected() ){
convertTemp = ( int ) ( 9.0f / 5.0f * temp + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
}

else if ( celsiusBoxFrom.isSelected() && kelvinBoxTo.isSelected()){
convertTemp = temp + 273;
temperature2.setText( String.valueOf( convertTemp ) );
}

else if ( kelvinBoxFrom.isSelected() && celsiusBoxTo.isSelected()){
convertTemp = temp - 273;
temperature2.setText( String.valueOf( convertTemp ) );
}

else if ( kelvinBoxFrom.isSelected() && fahrenheitBoxTo.isSelected()){
convertTemp = ( int ) ( 9.0f / 5.0f * ( temp - 273 ) + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
}
}
});

temperature2 = new JTextField( 10 );
temperature2.setEditable( false );

bottom = new JPanel();
bottom.setLayout(new GridLayout(1, 2));
bottom.add(label4);
bottom.add(temperature2);


Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add( top, BorderLayout.NORTH );
container.add( convertFrom, BorderLayout.WEST );
container.add( convertTo, BorderLayout.EAST );
container.add( bottom, BorderLayout.SOUTH );
setSize( 350,250);
setVisible( true );

}

public static void main ( String args[] ){
Convert application = new Convert();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}

最佳答案

BoxLayout 可以居中放置组件。 BoxLayout 遵循组件的 setAlignmentX 属性(另请参阅 this question 的答案)。
但有一个警告:如果组件配置为占据窗口的全部可用宽度(就像文本字段一样),则您必须管理组件的最大尺寸。以下是如何为“底部”面板执行此操作:

javax.swing.Box bottomBox = Box.createHorizontalBox();
bottom = new JPanel();
bottomBox.add(bottom);
bottom.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
bottom.add(label4);
temperature2.setMaximumSize(temperature2.getPreferredSize());
bottom.add(temperature2);
...
container.add( bottomBox, BorderLayout.SOUTH );

您可以为“顶部”面板做类似的事情。

关于java - 如何将已使用 borderlayout 布局的面板中的对象居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646514/

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