table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200,100));
我不知道如何解决这个问题,我似乎找不到一个会导致它失败的问题。下面是GUI代码的其余部分。它很长。将jtable添加到jpanel从第152行开始。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasql;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
/**
*
* @author KJ4CC
*/
public class UserInterface implements ActionListener {
DefaultTableModel dtm = new DefaultTableModel(0, 0);
public UserInterface() {
startGui();
}
JFrame frame = new JFrame();
Javasql sql = new Javasql();
JPanel buttom = new JPanel(new GridBagLayout());
JPanel commandPane = new JPanel(new GridBagLayout());
JPanel top = new JPanel(new GridBagLayout());
JPanel buttons = new JPanel(new GridBagLayout());
JPanel label = new JPanel(new GridBagLayout());
JButton connect = new JButton("Connect To Database");
JButton clr = new JButton("Clear Command");
JButton exeSql = new JButton("Execute SQL Command");
JButton clrRes = new JButton("Clear Result Window");
JLabel infoLabel = new JLabel("Enter Database Information ");
JLabel driverLabel = new JLabel("JDBC Driver: ");
JLabel dbLabel = new JLabel("Database URL: ");
JLabel userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
JLabel sqlLabel = new JLabel("Enter SQL Command: ");
JLabel connectionLabel = new JLabel("No Connection Now ");
JLabel exeLabel = new JLabel("SQL Execution Result: ");
//creating an instance of the new table
public JTable table;
public JScrollPane scrollPane;
JComboBox driverSelect = new JComboBox();
JComboBox url = new JComboBox();
JTextField username = new JTextField();
JTextField pass = new JTextField();
JTextArea command = new JTextArea(1, 1);
public void startGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
System.out.println("sdf");
c.insets = new Insets(0, 0, 0, 10);
c.fill = 0;
c.weightx = 1;
//adding all of the compoenets to their panel and then to the frame.
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(infoLabel, c);
c.gridx = 0;
c.gridy = 1;
top.add(driverLabel, c);
c.gridx = 1;
c.gridy = 1;
c.ipadx = 150;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(driverSelect, c);
c.gridx = 0;
c.gridy = 2;
c.fill = 0;
top.add(dbLabel, c);
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(url, c);
c.gridx = 0;
c.gridy = 3;
c.fill = 0;
c.fill = 0;
top.add(userLabel, c);
c.gridx = 1;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(username, c);
c.gridx = 0;
c.gridy = 4;
c.fill = 0;
c.fill = 0;
top.add(passLabel, c);
c.gridx = 1;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(pass, c);
//add the driver and url to the comboboxes
c.gridx = 2;
c.gridy = 0;
commandPane.add(sqlLabel, c);
c.gridx = 2;
c.gridy = 1;
c.ipadx = 150;
c.ipady = 75; //sql text area for command
c.fill = GridBagConstraints.FIRST_LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
commandPane.add(command, c);
c.insets = new Insets(0, 0, 0, 20);
c.ipadx = 9;
c.ipady = 1;
c.gridx = 0;
c.gridy = 0;
//buttons
label.add(connectionLabel, c);
c.gridx = 1;
c.gridy = 0;
//c.insets = new Insets(0, 0, 0, 50);
buttons.add(connect, c);
connect.addActionListener(this);
c.gridx = 2;
buttons.add(clr, c);
clr.addActionListener(this);
c.gridx = 3;
buttons.add(exeSql, c);
exeSql.addActionListener(this);
//adding the label and buttons above and below the tabel.
c.gridx = 0;
c.gridy = 1;
buttom.add(exeLabel, c);
c.gridx = 0;
c.gridy = 2;
//-----------------------------------------------------------------Table here
table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c);
buttom.add(scrollPane);
c.gridx = 0;
c.gridy = 3;
buttom.add(clrRes, c);
c.weightx = 2;
c.weighty = 2;
c.gridx = 0;
c.gridy = 0;
frame.setLayout(new GridLayout(3, 2));
frame.add(top);
c.gridx = 2;
c.gridy = 1;
frame.add(commandPane);
frame.add(label);
frame.add(buttons);
frame.add(buttom, BorderLayout.SOUTH);
//adding the content panel to the jframe.
frame.pack();
frame.setSize(1000, 550);
frame.setVisible(true);
//adding items to both of the combo boxes.
driverSelect.addItem("com.mysql.jdbc.Driver");
url.addItem("jdbc:mysql://localhost:3306/project3");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connect) {
sql.connect(this);
} else if (e.getSource() == clr) {
command.setText("");
} else if (e.getSource() == exeSql) {
sql.exeCommand(this);
}
}
}
不能将表添加两次:
table = new JTable();
scrollPane = new JScrollPane(table); //here
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c); //here
buttom.add(scrollPane);
如果将其添加到滚动窗格中,只需添加滚动窗格即可。组件不能有两个双亲。
我没有检查您的完整代码,但尝试
buttom.add(scrollPane,c);
buttom.add(table, c); //here
buttom.add(scrollPane);
通过单击Experiment Parameters选项卡中的一个按钮(参见下面的屏幕截图),我创建并运行一个“PreviewAction”,它创建了一个新的选项卡,并用必要的组件填充它。下面是的代码。编辑:我还发布了一个自包含的最小版本,它模拟了真实项目中的条件,并展示了相同的行为。 这里至少存在两个问题: (或)根本不呈现 没有框架本身那么宽,我不知道为什么 我在挥杆方面不是很好,所以我可能错过
一个没有显示添加到它的的问题。我已经尝试了,我已经切换到只添加,以使代码更短一些。任何帮助都是非常感谢的!
如果只是将JTable添加到JPanel中,默认情况下它不会显示头部。您应该将该表传递给JScrollPane的构造函数,或者使用该表作为参数调用JScrollPane。那么实际上,是什么使列标题可见呢?它是JScrollPane(和方法)内部呈现的一部分吗? 最初我认为JScrollPane使用它的来实现这一点,但它没有(将null传递给这个方法,表仍然会显示头部)。
我有一个显示良好的JTable。如果我将表放在JScrollPane中,它将不再显示。为什么不呢?我想在自己的JScrollPane中添加两个表。下面是我的代码,其中我只尝试向第一个表添加一个JScrollPane: 谢了。