如何使用布局获得相同的外观
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5,1));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel jtaPanel = new JPanel(new GridLayout(5,1));
jtaPanel.add(reSoulte);
Container pane = getContentPane();
pane.setLayout(null);
pane.add(panel);
pane.add(bpanel);
pane.add(jtfPanel);
pane.add(jtaPanel);
Insets insets = pane.getInsets();
setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);
Dimension size = panel.getPreferredSize();
panel.setBounds(0 + insets.left, 18 + insets.top,size.width, size.height);
size = bpanel.getPreferredSize();
bpanel.setBounds(0 + insets.left, 409 + insets.top,115+size.width, size.height);
size = jtfPanel.getPreferredSize();
jtfPanel.setBounds(180 + insets.left, 25 + insets.top,170 +size.width, size.height);
size = jtaPanel.getPreferredSize();
jtaPanel.setBounds(0 + insets.left, 150 + insets.top,265 +size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
System.exit(0);
}
}
}
最佳答案
你能告诉我如何将绝对位置替换为borderlayout
我看到您的代码的方式:
“面板”显示在左上方
“ jtfPanel”显示在右上角
中间显示“ jtaPanel”
“ bPanel”显示在底部
BorderLayout具有3个垂直区域,可与NORTH,CENTER和SOUTH一起使用,因此我建议您将“ panel”和“ jtfPanel”组合到另一个面板中,然后将3个面板添加到BorderLayout中。就像是:
JPanel north = new JPanel( new BorderLayout() );
north.add(panel, BorderLayout.WEST);
north.add(jtfPanel, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.add(north, BorderLayout.NORTH);
contentPane.add(jtaPanel, BorderLayout.CENTER);
contentPane.add(bPanel, BorderLayout.SOUTH);
要点是,您可以嵌套多个面板以达到所需的效果。
关于java - 使用borderlayout将组件添加到框架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20444414/