我很困惑。希望在这里有一个善良的人可以帮助我使用一行代码来使我的jtable进一步在面板上进一步延伸。目前它只是在屏幕中间保持非常小!我尝试了很多方法,最近才试图改变列的宽度(因为页面/ gui无论如何都是固定大小的)。无论如何,JTable的代码位于底部。我希望你能得到帮助!它只是一个方法和类,所以它很容易复制和粘贴和运行(如果你可以打扰,我明白,如果你不能!)
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
public class second extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public second()
{
}
public static void main(String[] args)
{
second sec = new second();
sec.createFrame();
}
@SuppressWarnings("deprecation")
private void createFrame()
{
JFrame frame = new JFrame("Second attempt");
JPanel content = new JPanel(new BorderLayout(5,10));
frame.getContentPane().add(content);
frame.setResizable(false);
JPanel requestor = new JPanel(new GridLayout(1, 4, 15, 20));
content.add("North", requestor);
requestor.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Requestor"));
JPanel col1 = new JPanel();
BoxLayout boxLayout1 = new BoxLayout(col1, BoxLayout.Y_AXIS);
col1.setLayout(boxLayout1);
JPanel col2 = new JPanel();
BoxLayout boxLayout2 = new BoxLayout(col2, BoxLayout.Y_AXIS);
col2.setLayout(boxLayout2);
JPanel col3 = new JPanel();
BoxLayout boxLayout3 = new BoxLayout(col3, BoxLayout.Y_AXIS);
col3.setLayout(boxLayout3);
JPanel col4 = new JPanel();
BoxLayout boxLayout4 = new BoxLayout(col4, BoxLayout.Y_AXIS);
col4.setLayout(boxLayout4);
requestor.add(col1);
requestor.add(col2);
requestor.add(col3);
requestor.add(col4);
//create JLabels
JLabel requestorId = new JLabel("RequestorID");
JLabel org = new JLabel("Organisation");
JLabel orgAddress = new JLabel("Organisation Address");
JLabel otherAdd = new JLabel("Other Address");
JLabel title = new JLabel("Title");
JLabel phoneNo = new JLabel("Phone Number");
JLabel firstName = new JLabel("First Name");
JLabel surname = new JLabel("Surname");
JLabel emailAdd = new JLabel("E Mail Address");
JLabel dept = new JLabel("Department");
JLabel fax = new JLabel("Fax Number");
JLabel spacer = new JLabel("Spacer");
//create text areas
JTextField reqIDTxt = new JTextField();
JTextField orgTxt = new JTextField();
JTextField orgAddTxt = new JTextField();
JTextField otherAddTxt = new JTextField();
JTextField titleTxt = new JTextField();
JTextField phoneNoTxt = new JTextField();
JTextField firstNameTxt = new JTextField();
JTextField faxNoTxt = new JTextField();
JTextField surnameTxt = new JTextField();
JTextField emailTxt = new JTextField();
JTextField deptTxt = new JTextField();
JTextField spacerTxt = new JTextField();
col1.add(requestorId);
col1.add(reqIDTxt);
col1.add(title);
col1.add(titleTxt);
col1.add(firstName);
col1.add(firstNameTxt);
col1.add(surname);
col1.add(surnameTxt);
col1.add(dept);
col1.add(deptTxt);
col2.add(org);
col2.add(orgTxt);
col2.add(phoneNo);
col2.add(phoneNoTxt);
col2.add(fax);
col2.add(faxNoTxt);
col2.add(emailAdd);
col2.add(emailTxt);
col2.add(spacer);
col2.add(spacerTxt);
col3.add(orgAddress);
col3.add(orgAddTxt);
col4.add(otherAdd);
col4.add(otherAddTxt);
JPanel buttonsPanel = new JPanel(new BorderLayout());
content.add("Center", buttonsPanel);
JPanel buttons = new JPanel(new FlowLayout());
buttonsPanel.add("North", buttons);
buttons.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));
JButton addBtn = new JButton("Add");
JButton deleteBtn = new JButton("Delete");
JButton clearAllBbtn = new JButton("Clear All");
JButton copyReqBtn = new JButton("Copy Requestor");
buttons.add(addBtn);
buttons.add(deleteBtn);
buttons.add(clearAllBbtn);
buttons.add(copyReqBtn);
JPanel checkBoxPanel = new JPanel(new BorderLayout());
buttonsPanel.add("Center", checkBoxPanel);
JPanel checkBoxHolder = new JPanel(new BorderLayout());
checkBoxPanel.add("North", checkBoxHolder);
JCheckBox chkBox = new JCheckBox("Search");
checkBoxHolder.add(chkBox);
JPanel bottomHalf = new JPanel(new BorderLayout());
checkBoxPanel.add("Center",bottomHalf );
//---------------------------------------------------------------------------------------------------------------------
Dimension d = new Dimension(100, 200);
JPanel splitForTable = new JPanel();
BoxLayout lay = new BoxLayout(splitForTable, BoxLayout.X_AXIS);
splitForTable.setLayout(lay);
bottomHalf.add("North",splitForTable );
JPanel rbtn = new JPanel();
BoxLayout layoutRbtn = new BoxLayout(rbtn, BoxLayout.Y_AXIS);
rbtn.setLayout(layoutRbtn);
rbtn.setPreferredSize(d);
splitForTable.add(rbtn);
rbtn.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Search Criteria"));
JTextField btnBox = new JTextField();
btnBox.setMaximumSize(new Dimension(10500, 20));
ButtonGroup group = new ButtonGroup();
btnBox.setColumns(15);
JRadioButton firstNameRbtn = new JRadioButton("First Name");
JRadioButton surnameRbtn = new JRadioButton("Surname");
JRadioButton orgNameRbtn = new JRadioButton("Organisation Name");
JRadioButton deptRbtn = new JRadioButton("Department");
JRadioButton addresRbtn = new JRadioButton("Address");
JRadioButton addresOtherRbtn = new JRadioButton("Address Other");
group.add(firstNameRbtn);
group.add(surnameRbtn);
group.add(orgNameRbtn);
group.add(deptRbtn);
group.add(addresRbtn);
group.add(addresOtherRbtn);
rbtn.add(btnBox);
rbtn.add(firstNameRbtn);
rbtn.add(surnameRbtn);
rbtn.add(orgNameRbtn);
rbtn.add(deptRbtn);
rbtn.add(addresRbtn);
rbtn.add(addresOtherRbtn);
rbtn.add(Box.createVerticalGlue());
JPanel jtablePanel = new JPanel(new FlowLayout());
splitForTable.add(jtablePanel);
jtablePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Results"));
//construct dummy data for table at the moment
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Ton", "Parker",
"Pool", new Integer(10), new Boolean(false)},
{"Sky", "Sports",
"Pool", new Integer(12), new Boolean(true)},
{"Ricki", "Lambert",
"Football", new Integer(10), new Boolean(true)},
{"Simon", "Gayton",
"Chemistry", new Integer(15), new Boolean(false)},
{"Tom", "Andrews",
"Golf", new Integer(45), new Boolean(false)},
{"Lionel", "Messi",
"Football", new Integer(10), new Boolean(true)},
{"Tom", "Andrews",
"Golf", new Integer(45), new Boolean(false)},
{"Tom", "Andrews",
"Golf", new Integer(45), new Boolean(false)},
{"Tom", "Andrews",
"Golf", new Integer(45), new Boolean(false)},
};
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
//THIS IS WHERE ALL TABLE INFORMATION IS USED
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(250, 200));
table.setFillsViewportHeight(true);
//scrollPane.add(table);
jtablePanel.add(scrollPane);
JPanel southBtn = new JPanel(new FlowLayout());
content.add("South", southBtn);
JButton bottomOkBtn = new JButton("Ok");
JButton bottomCancelBtn = new JButton("Cancel");
JButton bottomApplyBtn = new JButton("Apply");
southBtn.add(bottomOkBtn);
southBtn.add(bottomCancelBtn);
southBtn.add(bottomApplyBtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
答案 0 :(得分:1)
问题在于线路
scrollPane.setPreferredSize(new Dimension(250, 200));
您在此处设置滚动窗格的宽度。
我注释掉了这一行,表格填满了“结果”面板。
但:
代码中存在多种错误。
例如,最后一行中的frame.show();
:不推荐使用show方法。使用setVisible
方法
您正在使用JTextField
s作为地址,这意味着没有新行,整个地址将在一行中。尝试将JTextArea
与JScrollPane
可能有其他人,但我没有完全看完代码 如果您正在尝试快速开发UI,我建议使用带有拖放摆动开发的IDE(eclipse与visual editor或netbeans)
答案 1 :(得分:1)
Jtable有一个列模型
您可以在其上设置所需的列宽
TableColumnModel colModel=table_.getColumnModel();
TableColumn col=colModel.getColumn(i);
col.setPreferredWidth(w);