2016-03-07 50 views
0

Rough Idea Of What I'm Trying To Obtain最佳的布局管理器完成这一任务

我有(2)JTextAreas(输入文本&输出文本)

(2)JComboBoxes(自动检测& TranslateTo)

请看照片连接到看看目标是什么。

我想用某个布局管理器来获得这个布局我已经试过了FlowLayout,SpringLayout并且没有用这些获得理想的结果。这里是我的代码

public class translate extends JFrame implements ActionListener{ 
/* 
    Our Langauges of choice we can add more if desired in the future 
    */ 

    String[] nativeString = { "Auto-Detect"}; 
    String[] translateString = { "Español", "中文", "Francais", "Italiano", "Deutsche" , "English" }; 
    JComboBox nativeLanguage; 
    JComboBox translateTo; 
    JButton execute; 
     final JPanel topRight; 
     final JPanel topLeft; 
     final JPanel bottomLeft; 
     JPanel bottomRight; 
     JLabel tR; 
     JLabel tL; 
     JLabel bR; 
     JLabel bL; 


     JRadioButton local; 
     JRadioButton mainArea; 
     JTextArea input; 
     JTextArea output; 

    public translate() throws Exception{ 

     super("Translation Service"); 
     JOptionPane.showMessageDialog(null, "Translation Services Require A NetWork Function To Make Calls"); 
     Translate.setClientId("xxxxxxxxx"); 
     Translate.setClientSecret("Xxxxxxxxxxxx); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     setSize(700,400); 
     setLocationRelativeTo(null); 
     setResizable(true); 
     setLookAndFeel(); 
     SpringLayout spring = new SpringLayout(); 
     setLayout(spring); 


     input= new JTextArea(); 
     JScrollPane inScroll = new JScrollPane(); 
     input.add(inScroll); 
    input.setEditable(true); 
    input.setLineWrap(true); 


    execute = new JButton("Translate"); 
    execute.addActionListener(this); 
    output = new JTextArea(); 
    JScrollPane outScroll = new JScrollPane(); 
    output.add(outScroll); 
     output.setEditable(true); 
    output.setLineWrap(true); 
    output.setVisible(true); 
     /* 
     Our JComboBoxes for user to selct langauge they want to choose 
     */ 
     nativeLanguage = new JComboBox(nativeString); 
     translateTo = new JComboBox(translateString); 

     nativeLanguage.addActionListener(this); 
     translateTo.addActionListener(this); 
     // JLabels 

     tR = new JLabel("Auto-Detect"); 
     tL = new JLabel ("Target Langauge"); 
     // FlowLayout fl = new FlowLayout(); 
     SpringLayout sp = new SpringLayout(); 
     setLayout(sp); 
     // setLayout(fl); 
     //1 
     topRight = new JPanel(); 
     // topRight.setLayout(new BoxLayout(topRight, BoxLayout.PAGE_AXIS)); 
     topRight.add(nativeLanguage); 
     topRight.add(tR); 


     //2 
     topLeft = new JPanel(); 
     // topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.PAGE_AXIS)); 
     topLeft.add(translateTo); 
     topLeft.add(tL); 

     //3 

     bottomRight = new JPanel(); 
    // bottomRight.setLayout(new BoxLayout(bottomRight, BoxLayout.PAGE_AXIS)); 
     bottomRight.add(input); 

     //4 
     bottomLeft = new JPanel(); 
    // bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.LINE_AXIS)); 
    bottomLeft.add(output); 




    add(topRight, SpringLayout.NORTH); 
    add(topLeft, SpringLayout.NORTH); 
    add(bottomRight, SpringLayout.SOUTH); 
    add(bottomLeft, SpringLayout.SOUTH); 

    setVisible(true); 

    } 
+2

使用尽可能多的,因为你需要达到你所需要的结果,布局一般不是单一的水平,但我们构成了n深度的容器,每个集中在一个给定区域的个人需求,并建立起来,产生你想要的所有效果 – MadProgrammer

+0

感谢MadProgrammer,是的,我相信我已经包括这是最好的布局(网格布局)已经插入不同的布局,这似乎更容易在这种情况下工作和定制;试图调整一切在这一刻或至少我的jtextareas,因为看起来更像是textfields – TravJav92

+0

对于我的钱,我会使用复合'GridBagLayout's,但这就是我 – MadProgrammer

回答

0

70%完成一切都在每说的地方:(进口冷落)

public class translate extends JFrame implements ActionListener{ 
/* 
    Our Langauges of choice we can add more if desired in the future 
    */ 
    DaWord mainArea = new DaWord(); 
    JMenuBar mainBar; 
    JMenu menu; 
    JMenuItem main,saveAs; 

    String[] nativeString = { "Auto-Detect"}; 
    String[] translateString = { "Español", "中文", "Francais", "Italiano", "Deutsche" , "English" }; 
    JComboBox nativeLanguage; 
    JComboBox translateTo; 
    JButton execute; 
     final JPanel topRight; 
     final JPanel topLeft; 
     final JPanel bottomLeft; 
     JPanel bottomRight; 
     JLabel tR; 
     JLabel tL; 
     JLabel bR; 
     JLabel bL; 
     JScrollPane scroll1; 
     JScrollPane scroll2; 

     JRadioButton local; 
     JRadioButton mainAreas; 
     JTextArea input; 
     JTextArea output; 

    public translate() throws Exception{ 

     super("Translation Service"); 
     JOptionPane.showMessageDialog(null, "Translation Services Require A NetWork Function To Make Calls"); 
     Translate.setClientId("MYID"); 
     Translate.setClientSecret("shhItsASecret"); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     setSize(900,450); 
     setLocationRelativeTo(null); 
     setResizable(true); 
     setLookAndFeel(); 
     SpringLayout spring = new SpringLayout(); 
     setLayout(spring); 
    javax.swing.border.Border blackline = BorderFactory.createLineBorder(Color.black); 
    setBackground(new Color(128, 218, 235)); 
     TitledBorder title1 = BorderFactory.createTitledBorder(blackline, "Input Text"); 
     mainBar = new JMenuBar(); 
     menu = new JMenu("Options"); 

     main = new JMenuItem("Export To Main"); 
     saveAs = new JMenuItem("Save As"); 
     menu.add(main); 
     menu.add(saveAs); 
     mainBar.add(menu); 
    // add(mainBar); 

    input= new JTextArea(35,35); 
    scroll1 = new JScrollPane(input); 
    // scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     //add("Center", scroll1); 
    input.setEditable(true); 
    input.setLineWrap(true); 
    execute = new JButton("Translate"); 
    execute.addActionListener(this); 

    TitledBorder title2 = BorderFactory.createTitledBorder(blackline, "Output Text");     
    output = new JTextArea(35,35); 
    scroll2 = new JScrollPane(output); 
     // scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

    output.setEditable(true); 
    output.setLineWrap(true); 
    output.setVisible(true); 
     /* 
     Our JComboBoxes for user to selct langauge they want to choose 
     */ 
     nativeLanguage = new JComboBox(nativeString); 
     translateTo = new JComboBox(translateString); 

     nativeLanguage.addActionListener(this); 
     translateTo.addActionListener(this); 
     // JLabels 

     tR = new JLabel("Auto-Detect"); 
     tL = new JLabel ("Target Langauge"); 
     // 0,2 (2) rows across 
    GridLayout flo = new GridLayout(0,2); 
    setLayout(flo); 
     // setLayout(fl); 
     //1 
     topRight = new JPanel(); 
     // topRight.setLayout(new BoxLayout(topRight, BoxLayout.PAGE_AXIS)); 
     topRight.add(nativeLanguage); 
     topRight.add(tR); 


     //2 
     topLeft = new JPanel(); 
     // topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.PAGE_AXIS)); 
     topLeft.add(translateTo); 
     topLeft.add(tL); 

     //3 

     bottomRight = new JPanel(); 
    // bottomRight.setLayout(new BoxLayout(bottomRight, BoxLayout.PAGE_AXIS)); 
     bottomRight.add(input); 

     //4 
     bottomLeft = new JPanel(); 
    // bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.LINE_AXIS)); 
    bottomLeft.add(output); 



    setJMenuBar(mainBar); 
    add(topRight); 
    add(topLeft); 
    add(bottomRight); 
    bottomRight.setBorder(title1); 
    add(bottomLeft); 
    bottomLeft.setBorder(title2); 

    setVisible(true); 


    saveAs.addActionListener(new java.awt.event.ActionListener(){ 
public void actionPerformed(java.awt.event.ActionEvent evt){ 
     try { 
      saveAsActionPerformed(evt); 
     } catch (Exception ex) { 
      Logger.getLogger(DaWord.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private void saveAsActionPerformed(ActionEvent evt) throws Exception{ 
     Object trans = evt.getSource(); 
     if(trans==saveAs){ 
      save(); 
     } 
    } 
    private void save() throws FileNotFoundException { 

     JFileChooser js2 = new JFileChooser(); 
     js2.setDialogTitle("Save As"); 
     js2.setCurrentDirectory(new File ("./")); 
     int result = js2.showSaveDialog(null); 
     if (result == JFileChooser.APPROVE_OPTION){ 
     String in1 = "This Is My Original Text:\n\n\n"; 
     String in2 = "This Is My Translated Text:\n\n\n"; 
     String content1 = input.getText(); 
     String content2 = output.getText(); 
     // all put in one 
     String completa = in1+content1+in2+content2; 
     File nef = js2.getSelectedFile(); 
     PrintWriter fw = new PrintWriter(nef.getPath()); 
     fw.write(completa); 
     fw.flush(); 

     } 
      } 

}); 
    main.addActionListener(new java.awt.event.ActionListener(){ 
    public void actionPerformed(java.awt.event.ActionEvent evt){ 
     try { 
      mainActionPerformed(evt); 
     } catch (Exception ex) { 
      Logger.getLogger(DaWord.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private void mainActionPerformed(ActionEvent evt) throws Exception{ 
     Object mainS = evt.getSource(); 
     if(mainS==main){ 
      transfer(); 
     } 
    } 

      private void transfer() { 
      String original = input.getText(); 
      String trans = output.getText(); 


      String intro1 = "This Is Your Original Text Below:\n\n\n"; 
      String intro2 = "This Is Your Translated Text Below:\n\n\n"; 


      mainArea.userField.append(intro1+original); 
      mainArea.userField.append(intro2+trans); 

      } 

}); 





    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

    Object source = e.getSource(); 
    /* 
     JComboBox translateTo = (JComboBox)e.getSource(); 
    String box1 = (String) translateTo.getSelectedItem(); 

    */ 

    JComboBox nativeLanguage = (JComboBox) e.getSource(); 
    String newSelection = (String) translateTo.getSelectedItem(); 

    if (newSelection =="Español"){ 

     try { 
      eng2spanish(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 
}else if(newSelection=="Italiano"){ 
     try { 
      eng2ital(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 
}else if(newSelection=="Francais"){ 
     try { 
      eng2french(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 
}else if(newSelection=="中文"){ 
     try { 
      eng2chin(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 
}else if(newSelection=="Duetche"){ 
     try { 
      eng2ger(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 


}else if(newSelection =="English"){ 
     try { 
      eng2eng(); 
     } catch (Exception ex) { 
      Logger.getLogger(translate.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 



    } 


private void setLookAndFeel() { 
try{ 
UIManager.setLookAndFeel("UIManager.getSystemLookAndFeelClassName"); 
}catch (ClassNotFoundException | InstantiationException | IllegalAccessException | 
UnsupportedLookAndFeelException e){ 
} 
} 

     private void eng2spanish() throws Exception { 

String userIn = input.getText(); 
String spanishTranslation = Translate.execute(userIn, Language.SPANISH); 
output.setText(spanishTranslation); 
System.out.println(spanishTranslation); 


    } 

    private void eng2french() throws Exception { 

String userIn = input.getText(); 
String frenchTranslation = Translate.execute(userIn, Language.FRENCH); 
output.setText(frenchTranslation); 

    } 

    private void eng2chin() throws Exception { 

String userIn = input.getText(); 
String chinTranslation = Translate.execute(userIn, Language.CHINESE_TRADITIONAL); 
output.setText(chinTranslation); 

    } 

    private void eng2ger() throws Exception { 

String userIn = input.getText(); 
String germanTranslation = Translate.execute(userIn, Language.GERMAN); 
output.setText(germanTranslation); 

    } 



    private void eng2ital() throws Exception { 

String userIn = input.getText(); 
String italianTranslation = Translate.execute(userIn, Language.ITALIAN); 
output.setText(italianTranslation); 

    } 
     private void eng2ind() throws Exception { 

String userIn = input.getText(); 
String hindiTranslation = Translate.execute(userIn, Language.HINDI); 
output.setText(hindiTranslation); 

    } 

    private void eng2eng() throws Exception { 

String userIn = input.getText(); 
String engTranslation = Translate.execute(userIn, Language.ENGLISH); 
output.setText(engTranslation); 
    } 
} 
+0

如果任何人有任何建议像往常一样愿意听到他们!请记住,我知道有一些东西在那里是垃圾,并且很快就会开始管理家务:) – TravJav92

0

我认为GridLayout适合你。如果你想更灵活,你也可以使用GridBagLayoutHere是指向所有布局管理器和链接示例的链接。

但是你真的想这么做吗?看起来你已经开始使用GUI项目的一部分了,所以也许你应该考虑使用javafx而不是swing。

+0

谢谢你,我决定和这个一起做一个预感,然后我最终会使用这个人,但是你用你的意见巩固了我的决定,似乎比其他布局经理更快地达到预期的效果。我以后会发布给未来的问题求职者我使用的代码,当一切都组织:) – TravJav92