簡體   English   中英

使用Java 1.6中的Swing庫構建一個簡單的GUI

[英]Building a simple GUI using the Swing library in Java 1.6

我正在嘗試使用Swing庫構建一個簡單的GUI。 我不明白為什么我的表正在刪除之前添加到GUI的所有內容,然后才創建表。 我假設它是addMainPanel中的某個命令,但我不確定是哪個。 非常感謝您的建議。

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

    }
}

您的GUI可能是:

在此輸入圖像描述

如果你改變了

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

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

至:

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

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

因為在BorderLayout中只有一個JComponent可以放在其中一個區域中,因為沒有BorderLayout常量的定義,所以JComponent將放在BorderLayout.CENTER區域中。

3)

然后你必須改變

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

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

JFrame的默認布局是BorderLayout ,因此您需要在addTableaddMainPanel方法中分別使用以下addMainPanel方法:

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

請注意,不需要getContentPane()

你正在混合布局,組件......

如果要將面板和表格添加到框架中作為兩個單獨的組件(而不是面板內的表格),應該注意框架的布局, BorderLayout 您應該使用布局指示將面板和表格添加到框架中以放置組件,例如:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM