1

我正在开发一个 GUI,但是我正在尝试格式化以使其看起来对用户更具吸引力;我遇到了一些障碍,一个是我不知道如何在现有的 JPanel 中正确地将 JButtons 居中,我已经设法将它们排列成行,我想知道更多关于我如何真正做到格式化这些。(如果您有兴趣帮助我解决其他问题,请看这里,如何将输出流设置为 TextArea

这就是我目前所拥有的。

如果您单击链接,它将向您显示我当前在我的一个选项卡中拥有的内容,我希望图片中的 JButtons 以 X 轴为中心而不是左侧,因为它们是我想的默认设置.

所以这是我的代码,它垂直排列,但我需要所有行都居中。

package guipanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame {
    /**
     * Creates new form GUIPanel
     */
    public GUIPanel() {
        initComponents();
    }
    private void setOutputStream(boolean catchErrors) throws IOException {
        System.setOut(new PrintStream(new FilterOutputStream(new ByteArrayOutputStream()))); // catches System.out messages
        setVisible(true);
        requestFocus();
        if (catchErrors) {
               System.setErr(new PrintStream(new FilterOutputStream(new ByteArrayOutputStream())));
        }
    }
    private void setPosition(JPanel jPanel) {
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = gbc.CENTER;
        layout.setConstraints(jPanel, gbc);
        jPanel.setLayout(layout);
    }
    private void addTabs(JTabbedPane jTabbedPane1) throws IOException {
        jTabbedPane1.add("Main", jPanel);
        jPanel.add(textArea1);
        jTabbedPane1.add("Commands", jPanel1);
        jTabbedPane1.add("Rules", jPanel2);
        jTabbedPane1.add("Links", jPanel3);
        jPanel3.add(website);
        jPanel3.add(jSeparator1);
        jPanel3.add(youtube);
        jPanel3.add(jSeparator2);
        jPanel3.add(webclient);
        jPanel3.add(jSeparator4);
        jPanel3.add(forums);
        jTabbedPane1.add("Information", jPanel4);
        jTabbedPane1.add("Shutdown", jPanel5);
        jPanel5.add(Shutdown);
        jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.PAGE_AXIS));
        setPosition(jPanel5);
        setOutputStream(true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jLabel1 = new javax.swing.JLabel();
        website = new javax.swing.JButton();
        youtube = new javax.swing.JButton();
        forums = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        webclient = new javax.swing.JButton();
        Shutdown = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        textArea1.setPreferredSize(new java.awt.Dimension(432, 300));
        textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);

        jLabel1.setText("Evolution-X™ Programmed by Musicmasta.");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        website.setText("Website");
        website.setMaximumSize(new java.awt.Dimension(133, 37));
        website.setPreferredSize(new java.awt.Dimension(133, 37));
        website.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        website.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                websiteActionPerformed(evt);
            }
        });

        youtube.setText("Youtube");
        youtube.setMaximumSize(new java.awt.Dimension(133, 37));
        youtube.setPreferredSize(new java.awt.Dimension(133, 37));
        youtube.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                youtubeActionPerformed(evt);
            }
        });

        forums.setText("Forums");
        forums.setMaximumSize(new java.awt.Dimension(133, 37));
        forums.setPreferredSize(new java.awt.Dimension(133, 37));
        forums.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        forums.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forumsActionPerformed(evt);
            }
        });
        forums.getAccessibleContext().setAccessibleName("Forums");

        webclient.setText("Webclient");
        webclient.setMaximumSize(new java.awt.Dimension(133, 37));
        webclient.setName(""); // NOI18N
        webclient.setPreferredSize(new java.awt.Dimension(133, 37));
        webclient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                webclientActionPerformed(evt);
            }
        });
        webclient.getAccessibleContext().setAccessibleName("");

        Shutdown.setText("Shutdown");
        Shutdown.setMaximumSize(new java.awt.Dimension(221, 81));
        Shutdown.setPreferredSize(new java.awt.Dimension(221, 81));
        Shutdown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShutdownActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evolution-X 639");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setPreferredSize(new java.awt.Dimension(450, 420));
        getContentPane().setLayout(new java.awt.FlowLayout());

        try {
            addTabs(jTabbedPane1);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        jTabbedPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTabbedPane1.setFocusable(false);
        jTabbedPane1.setOpaque(true);
        getContentPane().add(jTabbedPane1);
        jTabbedPane1.getAccessibleContext().setAccessibleName("");

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.exit(0);
    }                                      

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    private void youtubeActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            youtubelink = new URI("http://youtube.com/user/theevolutionx10");
            try {
                desktop.browse(youtubelink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void websiteActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            websitelink = new URI("http://evolutionx.x10.mx/");
            try {
                desktop.browse(websitelink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void forumsActionPerformed(java.awt.event.ActionEvent evt) {

        Desktop desktop = Desktop.getDesktop();
        try {
            forumlink = new URI("http://www.evolutionx.x10.mx/forum/index.php");
            try {
                desktop.browse(forumlink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void webclientActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            webclientlink = new URI("http://evolutionx.x10.mx/play.html");
            try {
                desktop.browse(webclientlink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void ShutdownActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton Shutdown;
    private javax.swing.JButton forums;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.TextArea textArea1;
    private javax.swing.JButton webclient;
    private javax.swing.JButton website;
    private javax.swing.JButton youtube;
    // End of variables declaration
    private JPanel jPanel = new JPanel();
    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();
    private JPanel jPanel3 = new JPanel();
    private JPanel jPanel4 = new JPanel();
    private JPanel jPanel5 = new JPanel();
    private URI webclientlink, youtubelink, forumlink, websitelink;

}
4

1 回答 1

3

您只需在将按钮添加到选项卡之前添加此行:

website.setAlignmentX(Component.CENTER_ALIGNMENT);
youtube.setAlignmentX(Component.CENTER_ALIGNMENT);
webclient.setAlignmentX(Component.CENTER_ALIGNMENT);
forums.setAlignmentX(Component.CENTER_ALIGNMENT);
于 2012-10-19T00:19:43.510 回答