我的JFRAME布局有问题
布局对我来说非常困难,因为我刚开始学习它。
我正在寻找的布局是
选择数据START
DAY:" textField"
月:" textField"
年:" textField"
结束日期
DAY:" textField"
月:" textField"
年:" textField"
就像它处于最后阶段一样,为什么不能正常工作
以下代码显示上面的图片
import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
public class TopUpHistoryScreen extends JDialog {
private JPanel mainPanel;
private JTextArea historyScreen;
public TopUpHistoryScreen()
{
setPanels();
setModalityType(ModalityType.APPLICATION_MODAL);
setSize(600, 600);
setVisible(true);
}
public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JPanel lowerPanel = new JPanel(new FlowLayout());
//JButton apply = new JButton("Select data area");
JButton exit = new JButton("Okay!");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
JButton checkDate = new JButton("check dates");
JLabel SelectData = new JLabel("Select data area\n");
JLabel START = new JLabel("START DATE!");
JLabel startDay = new JLabel("Day:");
JTextField sDay = new JTextField();
JLabel startMonth = new JLabel("Month:");
JTextField sMonth = new JTextField();
JLabel startYear = new JLabel("Year:");
JTextField sYear = new JTextField();
JLabel END = new JLabel("END DATE!");
JLabel endDay = new JLabel("Day:");
JTextField eDay = new JTextField();
JLabel endMonth = new JLabel("Month:");
JTextField eMonth = new JTextField();
JLabel endYear = new JLabel("Year:");
JTextField eYear = new JTextField();
JTextField Data = new JTextField();
JTextField touchOnTimeFieldminute = new JTextField();
historyScreen = new JTextArea(5,30);
JScrollPane scrolll = new JScrollPane(historyScreen);
mainPanel.add(SelectData);
mainPanel.add(START);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(END);
mainPanel.add(endDay);
mainPanel.add(eDay);
mainPanel.add(endMonth);
mainPanel.add(eMonth);
mainPanel.add(endYear);
mainPanel.add(eYear);
mainPanel.add(checkDate);
//mainPanel.add(touchOnTimeFieldhour);
//mainPanel.add(SelectData);
//mainPanel.add(touchOnTimeFieldminute);
//mainPanel.add(touchOnTimem);
lowerPanel.add(scrolll);
lowerPanel.add(exit);
//lowerPanel.add(apply);
//touchOnTimeFieldhour.setSize(10,10);
containerPanel.add(mainPanel);
containerPanel.add(lowerPanel);
add(containerPanel);
}
}
答案 0 :(得分:2)
将mainPanel.add(component);
方法中的setPanels()
序列更改为以下内容..
mainPanel.add(SelectData);
mainPanel.add(START);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(sMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(END);
mainPanel.add(new JLabel());
mainPanel.add(endDay);
mainPanel.add(eDay);
mainPanel.add(endMonth);
mainPanel.add(eMonth);
mainPanel.add(endYear);
mainPanel.add(eYear);
mainPanel.add(checkDate);
并且它会工作..当您使用GridView
到mainPanel
时,元素将根据您将其添加到mainPanel
的序列来获取位置...