2011-09-18 94 views
3

我想使用Swing库构建一个简单的GUI。我不明白为什么我的表正在擦除以前添加到GUI的所有内容,然后才创建表。我假设它是addMainPanel中的某个命令,但我不确定哪个。您的建议将不胜感激。使用Java 1.6中的Swing库构建一个简单的GUI

package fuelConsumption; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class LogView implements ActionListener { 

    private Log myLog; 
    private JFrame frame; 

    public LogView (String frameName) { 
     this.frame = new JFrame(frameName); 
     this.frame.setPreferredSize(new Dimension(500,500)); 
     this.frame.getContentPane().setLayout(new BorderLayout()); 

     this.addMainPanel(frame); 
     this.addTable(frame); 
     //addMenu(frame); 
     //addToolBar(frame); 

     this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.frame.pack(); 
     this.frame.setVisible(true); 
    } 

    private void addTable(JFrame frame2) { 
     String[] columnNames = {"date", 
           "station", 
           "fuel grade", 
           "fuel amount", 
           "fuel unit cost", 
           "fuel cost", 
           "trip distance"}; 
     Object[][] data = { 
      {"Shell", 89, 40, 109.5, "bla", 100, 123} 
     }; 

     JTable table = new JTable(data, columnNames); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     //table.setFillsViewportHeight(true); 

     //Create the scroll pane and add the table to it. 
     JScrollPane scrollPane = new JScrollPane(table); 

     //Add the scroll pane to this panel. 
     //this.frame.setContentPane(scrollPane); 
     frame2.getContentPane().add(scrollPane); 
    } 

    private void addMainPanel(JFrame frame2) { 
     // TODO Auto-generated method stub 

     JPanel panel = new JPanel(new GridBagLayout()); 

     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 5; 
     c.ipady = 50; 
     c.anchor = GridBagConstraints.LINE_START; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     JLabel label = new JLabel(""); 
     panel.add(label,c); 

     label = new JLabel("Info"); 
     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.LINE_START; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(label,c); 

     label = new JLabel("Label"); 
     c = new GridBagConstraints(); 
     c.gridx = 2; 
     c.gridy = 1; 
     c.anchor = GridBagConstraints.LINE_START; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(label,c); 

     label = new JLabel("Comments"); 
     c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 2; 
     c.anchor = GridBagConstraints.FIRST_LINE_START; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(label,c); 

     JTextArea textArea = new JTextArea(4,30); 
     JScrollPane textScroll = new JScrollPane(textArea); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 2; 
     c.gridwidth = 4; 
     c.ipadx = 30; 
     c.ipady = 50; 
     c.anchor = GridBagConstraints.FIRST_LINE_START; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(textScroll,c); 

     JButton button = new JButton("Edit"); 
     button.addActionListener(this); 
     button.setActionCommand("Edit"); 
     c = new GridBagConstraints(); 
     c.gridx = 1; 
     c.gridy = 3; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(button,c); 

     button = new JButton("Previous"); 
     button.addActionListener(this); 
     button.setActionCommand("Previous"); 
     c = new GridBagConstraints(); 
     c.gridx = 2; 
     c.gridy = 3; 
     c.weightx = 0.5; 
     c.weighty = 0.5; 
     panel.add(button,c); 

     button = new JButton("Next"); 
     button.addActionListener(this); 
     button.setActionCommand("Next"); 
     c = new GridBagConstraints(); 
     c.gridx = 3; 
     c.gridy = 3; 
     //  c.weightx = 0.5; 
     //  c.weighty = 0.5; 
     panel.add(button,c); 

     frame2.getContentPane().add(panel); 
    } 

    public static void main(String [] args){ 
     new LogView("Fuel Consumption"); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 

回答

6

你的GUI可以是:

enter image description here

如果更改

private void addTable(JFrame frame2) {private void addTable() {

frame2.getContentPane().add(scrollPane);frame.add(scrollPane, BorderLayout.CENTER);

到:

private void addMainPanel(JFrame frame2) {private void addMainPanel() {

frame2.getContentPane().add(panel);frame.add(panel, BorderLayout.SOUTH);

由于BorderLayout只有一个JComponent可以放置在的地区之一,在目前还没有对BorderLayout的定义常量,那么JComponent将被放置在BorderLayout.CENTER区域。

3)

然后,你必须改变

this.addMainPanel(frame); 
this.addTable(frame); 

this.addMainPanel(); 
this.addTable(); 
+0

+1正如你观察到,让遏制层次符合词法范围更为常见,例如返回一个面板而不是传递一个容器。 – trashgod

4

JFrame的默认布局是BorderLayout,所以你需要分别在addTableaddMainPanel方法类似这些,:

frame2.add(scrollPane, BorderLayout.CENTER); 
frame2.add(panel, BorderLayout.SOUTH); 

注意getContentPane()不是必需的。

+0

我+1清晰和简单的答案 – mKorbel

2

你混合布局,组件...

如果要添加的面板将表格作为两个独立的组件(而不是面板内的表格),应该注意布局框架,BorderLayout。您应该添加的面板和表使用的适应症布局框架放置组件,如:

frame2.getContentPane().add(scrollPane, BorderLayout.SOUTH); 

frame2.getContentPane().add(panel, BorderLayout.NORTH);