2010-06-15 44 views
3

我一直在尝试左对齐包含在Box中的按钮到左侧,但没有成功。Java Box Class:Unsolvable:将组件向左或向右对齐

它们左对齐,但出于某种原因,不会像所想象的那样左移。

我附上下面的代码。请尝试编译并亲自查看。对我来说似乎很奇怪。

谢谢,埃里克

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class MainGUI extends Box implements ActionListener{ 
    //Create GUI Components 
    Box centerGUI=new Box(BoxLayout.X_AXIS); 
    Box bottomGUI=new Box(BoxLayout.X_AXIS); 
    //centerGUI subcomponents 
    JTextArea left=new JTextArea(), right=new JTextArea(); 
    JScrollPane leftScrollPane = new JScrollPane(left), rightScrollPane = new JScrollPane(right); 
    //bottomGUI subcomponents 
    JButton encrypt=new JButton("Encrypt"), decrypt=new JButton("Decrypt"), close=new JButton("Close"), info=new JButton("Info"); 
    //Create Menubar components 
    JMenuBar menubar=new JMenuBar(); 
    JMenu fileMenu=new JMenu("File"); 
    JMenuItem open=new JMenuItem("Open"), save=new JMenuItem("Save"), exit=new JMenuItem("Exit"); 
    int returnVal =0; 

    public MainGUI(){ 
     super(BoxLayout.Y_AXIS); 
     initCenterGUI(); 
     initBottomGUI(); 
     initFileMenu(); 
     add(centerGUI); 
     add(bottomGUI); 
     addActionListeners(); 
    } 
    private void addActionListeners() { 
     open.addActionListener(this); 
     save.addActionListener(this); 
     exit.addActionListener(this); 
     encrypt.addActionListener(this); 
     decrypt.addActionListener(this); 
     close.addActionListener(this); 
     info.addActionListener(this); 
    } 
    private void initFileMenu() { 
     fileMenu.add(open); 
     fileMenu.add(save); 
     fileMenu.add(exit); 
     menubar.add(fileMenu); 
    } 
    public void initCenterGUI(){ 
     centerGUI.add(leftScrollPane); 
     centerGUI.add(rightScrollPane); 
    } 
    public void initBottomGUI(){ 
     bottomGUI.setAlignmentX(LEFT_ALIGNMENT); 
     //setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     bottomGUI.add(encrypt); 
     bottomGUI.add(decrypt); 
     bottomGUI.add(close); 
     bottomGUI.add(info); 
    } 
    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // find source of the action 
     Object source=arg0.getSource(); 
     //if action is of such a type do the corresponding action 
     if(source==close){ 
      kill(); 
     } 
     else if(source==open){ 
      //CHOOSE FILE 
      File file1 =chooseFile(); 
      String input1=readToString(file1); 
      System.out.println(input1); 
      left.setText(input1); 
     } 
     else if(source==decrypt){ 
      //decrypt everything in Right Panel and output in left panel 
      decrypt(); 
     } 
     else if(source==encrypt){ 
      //encrypt everything in left panel and output in right panel 
      encrypt(); 
     } 
     else if(source==info){ 
      //show contents of info file in right panel 
      doInfo(); 
     } 
     else { 
      System.out.println("Error"); 
      //throw new UnimplementedActionException(); 
     } 
    } 

    private void doInfo() { 
     // TODO Auto-generated method stub 
    } 
    private void encrypt() { 
     // TODO Auto-generated method stub 
    } 
    private void decrypt() { 
     // TODO Auto-generated method stub 
    } 
    private String readToString(File file) { 
     FileReader fr = null; 
     try { 
      fr = new FileReader(file); 
     } catch (FileNotFoundException e1) { 
      e1.printStackTrace(); 
     } 
     BufferedReader br=new BufferedReader(fr); 

     String line = null; 
     try { 
      line = br.readLine(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String input=""; 
     while(line!=null){ 
      input=input+"\n"+line; 
      try { 
       line=br.readLine(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return input; 
    } 
    private File chooseFile() { 
     //Create a file chooser 
    final JFileChooser fc = new JFileChooser(); 
    returnVal = fc.showOpenDialog(fc); 
     return fc.getSelectedFile(); 
    } 
    private void kill() { 
     System.exit(0); 
    } 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     MainGUI test=new MainGUI(); 
     JFrame f=new JFrame("Tester"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setJMenuBar(test.menubar); 
     f.setPreferredSize(new Dimension(600,400)); 
     //f.setUndecorated(true); 
     f.add(test); 
     f.pack(); 
     f.setVisible(true); 
    } 

}

回答

2

阅读从部分有关How to Use Box Layout的Swing教程。它解释了BoxLayout在组件具有不同对齐时的工作原理(并举例说明)。

简单的解决方案是增加:

centerGUI.setAlignmentX(LEFT_ALIGNMENT); 
+0

这个工作。看起来有点不直观,但我会读文本,看看为什么。 – user323186 2010-06-16 07:47:10

+1

与此同时,如果有人希望挽救他人阅读全文,并认为他们可以解释它,请添加一个帖子。 – user323186 2010-06-16 07:54:14

0

为什么不尝试使用MiGLayout

他们有很多很棒的在线演示源代码,包括很多不同的对齐示例。

+0

愚蠢的答案。只需更改布局管理器即可。 – user323186 2010-06-16 07:49:38

+0

MiGLayout *是一个布局管理器:-)虽然你是对的,但使用 – mikera 2010-06-16 11:12:55

2

不知道为什么按钮按照它们的方式排列,但可能是因为它们试图与上面的框对齐(我确信有人更熟悉Swing会给你更好的答案) 。调试布局问题的简便方法是突出显示具有彩色边框的组件,例如在当前的代码:

centerGUI.setBorder(BorderFactory.createLineBorder(Color.GREEN)); 
    add(centerGUI); 
    bottomGUI.setBorder(BorderFactory.createLineBorder(Color.RED)); 
    add(bottomGUI); 

但是,如果我有这些要求,我会使用一个BorderLayout,例如这段代码是松散的基础上你的,我删除了不必要的位,集中布局部分(提问的时候,你应该这样做,它可以让别人更容易回答的问题)

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class Tester { 

    private JPanel contentPanel; 
    private JTextArea leftTextArea = new JTextArea(); 
    private JTextArea rightTextArea = new JTextArea(); 
    private JMenuBar menuBar = new JMenuBar(); 

    public Tester() { 
     initialisePanel(); 
     initFileMenu(); 
    } 

    public JPanel getContent() { 
     return contentPanel; 
    } 

    public JMenuBar getMenuBar() { 
     return menuBar; 
    } 

    private final void initialisePanel() { 
     contentPanel = new JPanel(new BorderLayout()); 

     Box centreBox = new Box(BoxLayout.X_AXIS); 
     JScrollPane leftScrollPane = new JScrollPane(leftTextArea); 
     JScrollPane rightScrollPane = new JScrollPane(rightTextArea); 
     centreBox.add(leftScrollPane); 
     centreBox.add(rightScrollPane); 

     Box bottomBox = new Box(BoxLayout.X_AXIS); 
     bottomBox.add(new JButton(new SaveAction())); 
     bottomBox.add(new JButton(new ExitAction())); 
     contentPanel.add(centreBox, BorderLayout.CENTER); 
     contentPanel.add(bottomBox, BorderLayout.SOUTH); 
    } 

    private void initFileMenu() { 
     JMenu fileMenu = new JMenu("File"); 
     fileMenu.add(new SaveAction()); 
     fileMenu.add(new ExitAction()); 
     menuBar.add(fileMenu); 
    } 

    class SaveAction extends AbstractAction { 
     public SaveAction() { 
      super("Save"); 
     } 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      handleSave(); 
     } 
    } 

    void handleSave() { 
     System.out.println("Handle save"); 
    } 

    class ExitAction extends AbstractAction { 
     public ExitAction() { 
      super("Exit"); 
     } 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      handleExit(); 
     } 
    } 

    void handleExit() { 
     System.out.println("Exit selected"); 
     System.exit(0); 
    } 

    public static void main(String[] args) { 
     Tester test = new Tester(); 
     JFrame frame = new JFrame("Tester"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setContentPane(test.getContent()); 
     frame.setJMenuBar(test.getMenuBar()); 
     frame.setPreferredSize(new Dimension(600, 400)); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

} 
+1

+1非常容易,这是出色的调试技术 – 2014-07-30 05:35:58