Build a simple GUI using the swing Library in Java 1.6

I'm trying to build a simple GUI using the swing library I don't understand why my table is deleting everything previously added to the GUI before creating the table I assume it's a command in addmainpanel, but I'm not sure which one Thank you very much for your suggestion

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

    }
}

Solution

Your GUI might be:

If you change

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

and

frame2. Getcontentpane() add (scroll panel) to frame. add(scrollPane,BorderLayout.CENTER);

To:

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

and

. frame2. Getcontentpane() add (panel); to frame. add(panel,BorderLayout.soUTH);

Because only one JComponent can be placed in one of the areas in the borderlayout, because there is no definition of the borderlayout constant, the JComponent will be placed in the borderlayout In the center area

3)

Then you have to change

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

to

this.addMainPanel();
this.addTable();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>