以下代码有效,但仅在按下显示值和调整大小按钮之后。直到我按下显示值并按下调整大小按钮后,它才会出现。仅按下“显示值”按钮时如何使其显示?
public class myClass extends JFrame{
String[] colNames = {"Item Name",
"Department",
"Original Price",
"Sales Price"};
Object[][] input = {
{"Kathy", "Smith",
new Double(10), new Integer(5)},
{"John", "Doe",
new Double(10), new Integer(3)},
{"Sue", "Black",
new Double(10), new Integer(2)},
{"Jane", "White",
new Double(10), new Integer(20)},
{"Joe", "Brown",
new Double(10), new Integer(10)}
};
//display values
JButton buttonDisplay = new JButton();
buttonDisplay.setText("Display Values");
container.add(buttonDisplay);
buttonDisplay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
JTable jt = new JTable( input, colNames );
JScrollPane pane = new JScrollPane( jt );
jt.setPreferredScrollableViewportSize(
new Dimension(200,200));
pane.add(jt);
container.add( pane );
container.add(jt);
/*Must use a JTextBox or JTable to display all the stored values for:
*
* Item name
Department
Original price
Sale price
*/
}
});