繁体   English   中英

完成此任务的最佳布局管理器

[英]Best Layout Manager For Accomplishing This Task

我想要获得的粗略想法

我有(2)个JTextAreas(输入文本和输出文本)

(2)JComboBoxes(AutoDetect和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);

    }

我认为GridLayout为您工作。 如果您想要更大的灵活性,也可以使用GridBagLayout 这里是所有布局管理器的链接和链接的示例。

但是,您是否真的想通过秋千做到这一点? 看来您刚开始使用项目的GUI部分,因此也许您应该考虑使用javafx而不是swing。

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); 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM