在GroupLayout中创建页脚



我有一个Java Swing应用程序。书面的我想像使用JPanel statusPanel一样给它添加一个页脚。我知道这可以在BorderLayout中完成,但我希望在GroupLayout中完成。目前我正在使用以下内容:

JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setText("Status Bar");

下面是我的完整代码

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import org.NXI.CreateNxi_Main;
import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JProgressBar;
@SuppressWarnings("serial")
public class DummyFrame extends JFrame {
    private JPanel contentPane;
    private JTextField srcTextField;
    private JTextField destTextField;
    private JButton btnNewButton;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DummyFrame frame = new DummyFrame();
                    frame.setResizable(false);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public DummyFrame() {
        setTitle("DummyFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 604, 452);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.showOpenDialog(null);
                File f = fChoose.getSelectedFile();
                srcTextField.setText(f.getAbsolutePath());
            }
        });
        srcTextField = new JTextField();
        srcTextField.setColumns(10);
        destTextField = new JTextField();
        destTextField.setColumns(10);
        JButton destBtn = new JButton("Output Excel");
        destBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fChoose.setAcceptAllFileFilterUsed(false);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    destTextField.setText(fChoose.getSelectedFile().getAbsolutePath() + "\");
                    System.out.println(fChoose.getCurrentDirectory());
                } else {
                    System.out.println("No Selection");
                }
            }
        });
        final JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        final JTextArea outputTextArea = new JTextArea();
        outputTextArea.setEditable(false);
        btnNewButton = new JButton("Convert");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CreateNxi_Main createNxi_Main = new CreateNxi_Main();
                try {
                    createNxi_Main.getInputAndOutputPaths(srcTextField.getText(), destTextField.getText() + "\",
                            outputTextArea, progressBar);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(outputTextArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(100, 100));
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setText("Developed by 0138039");
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
                                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(destTextField, Alignment.TRAILING,
                                                        GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                                        .addComponent(srcTextField, GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
                                .addGap(122))
                        .addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 543,
                                                Short.MAX_VALUE)
                                .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 543,
                                        Short.MAX_VALUE)
                                .addGroup(gl_contentPane.createSequentialGroup()
                                        .addPreferredGap(ComponentPlacement.RELATED, 446, Short.MAX_VALUE).addComponent(
                                                destBtn, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                        .addPreferredGap(ComponentPlacement.RELATED, 446, Short.MAX_VALUE).addComponent(
                                                srcBtn, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))))
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(46)
                                .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                        GroupLayout.PREFERRED_SIZE)
                                .addGap(36).addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                .addGap(218)))
                        .addGap(25)));
        gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane
                .createSequentialGroup().addGap(14)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(3).addComponent(srcTextField,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addComponent(srcBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE, false)
                        .addComponent(destTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                        .addComponent(destBtn, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(18).addComponent(btnNewButton,
                                GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(31).addComponent(progressBar,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addGap(18).addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8)));
        contentPane.setLayout(gl_contentPane);
    }
}

我不想使用标签,而是使用StatusPanel。请让我知道我是否可以得到这个,如果可以,我该怎么做?

Swing中没有StatusPanel这样的东西。在任何布局中,状态行通常只是JFrame底部的一条窄线。比JLabel更令人兴奋的方法是通过扩展JPanel来滚动您自己的StatusPanel。这就是他们在Sabacc Online中的做法:

public class JStatusPanel extends JPanel {
    protected JLabel message = new JLabel("Status");
    public JStatusPanel() {
        this.setLayout(new BorderLayout());
        message.setAlignmentY(JLabel.CENTER_ALIGNMENT);
        message.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        add(message, BorderLayout.CENTER);
    }
    public void showInfo(String text) {
        message.setForeground(new Color(0, 0, 0));
        message.setText(text);
    }
    public void showError(String text) {
        message.setForeground(new Color(255, 0, 0));
        message.setText(text);
    }
}

相关内容

  • 没有找到相关文章

最新更新