2015-09-08 46 views
-1

我想設置我的JTextArea佔用屏幕的最大horz長度,以便下一件事,在這種情況下,一個按鈕,將開始一個新的行,但我不知道如何做到這一點。我通過設置JTextArea的大小從20到1000變化,但沒有做任何事情。如何在Java SWING的新行上啓動一些東西?

如何讓我的textarea佔據整個第一行,然後在下一行開始添加下一個項目?以下是我迄今爲止...

MyFrame(){//constructor 

     super("Simple Calculator"); 
     p = new JPanel(); 
     grid = new GridLayout(4, 4, 3, 3); 
     p.setLayout(grid); 
     setSize(400, 500); 
     setResizable(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setUpTextScreen(); 
     //create buttons 
     for(int i = 0; i < buttonValues.length; i++){ 
      p.add(new JButton(buttonValues[i])); 
     } 
     add(p); 
     setVisible(true); 
    } 

    private void setUpTextScreen() { 

     textOnScreen = new JTextArea(7, 1000); 
     textOnScreen.setText("0");//default 
     textOnScreen.setEditable(false); 
     p.add(textOnScreen); 
    } 
+1

最簡單的方法是嘗試新的佈局。其他佈局風格是什麼? gridLayout和... boxLayout? – Coffee

+0

是否嘗試添加到文本'\ n'? –

+1

@NirLecy他想要一個*組件*直接在下一個「行」開始,而不是textarea內的文本。 –

回答

5

我怎樣才能讓我的文字區域佔用整個第一行,然後有下我在下一行添加的項目?

將佈局分解爲邏輯部分。從主面板開始,使用BorderLayout

  1. 首先,我將使用JTextField作爲計算器顯示,而不是JTextArea。然後,您可以使用以下方式添加文本字段:mainPanel.add(textField, BorderLayout.PAGE_START);

  2. 然後,您使用GridLayout爲按鈕創建一個JPanel。然後添加按鈕的按鈕面板,並使用:maonPanel.add(buttonPanel, BorderLayout.CENTER);

例如:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class CalculatorPanel extends JPanel 
{ 
    private JTextField display; 

    public CalculatorPanel() 
    { 
     Action numberAction = new AbstractAction() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
//    display.setCaretPosition(display.getDocument().getLength()); 
       display.replaceSelection(e.getActionCommand()); 
      } 
     }; 

     setLayout(new BorderLayout()); 

     display = new JTextField(); 
     display.setEditable(false); 
     display.setHorizontalAlignment(JTextField.RIGHT); 
     add(display, BorderLayout.NORTH); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new GridLayout(0, 5)); 
     add(buttonPanel, BorderLayout.CENTER); 

     for (int i = 0; i < 10; i++) 
     { 
      String text = String.valueOf(i); 
      JButton button = new JButton(text); 
      button.addActionListener(numberAction); 
      button.setBorder(new LineBorder(Color.BLACK)); 
      button.setPreferredSize(new Dimension(30, 30)); 
      buttonPanel.add(button); 

      InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
      inputMap.put(KeyStroke.getKeyStroke(text), text); 
      inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text); 
      button.getActionMap().put(text, numberAction); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("Calculator Panel"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new CalculatorPanel()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
4

哈瓦看看Nested layouts,你可以添加一個面板採用了BorderLayout的(還有其他的選擇太雖然)和文本區域添加到它。然後你只需要一個帶GridLayout的面板來顯示按鈕。這是一個例子:(注意,幾行是在這段代碼是不必要的,但他們幫助瞭解佈局)

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.border.LineBorder; 

public class Example extends JFrame { 

    Example() {// 

     super("Simple Calculator"); 

     // The Main Panel where the 2 other panels will be on 
     JPanel mainPanel = new JPanel(new BorderLayout()); 

     // The textarea will be inside this panel 
     JPanel areaPanel = new JPanel(new BorderLayout()); 

     JTextArea area = new JTextArea(
       "This is a JTextArea -Long text to show it works -Long text to show it works- -Long text to show it works- -Long text to show it works- -Long text to show it works- -Long text to show it works-"); 
     area.setBorder(new LineBorder(Color.BLACK)); 
     area.setWrapStyleWord(true); 
     area.setLineWrap(true); 

     // Fill the whole space of the panel with the area 
     areaPanel.add(area, BorderLayout.CENTER); 

     // The buttons will be inside this panel 
     JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 3, 3)); 
     for (int i = 0; i < 16; i++) { // Adding buttons 
      buttonPanel.add(new JButton("Button" + i)); 
     } 

     // The textarea-panel should be on top of the main panel 
     mainPanel.add(areaPanel, BorderLayout.NORTH); 

     // The panel with the buttons should fill the remaining space 
     mainPanel.add(buttonPanel, BorderLayout.CENTER); 

     getContentPane().add(mainPanel); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(500, 500); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new Example(); 
    } 

} 
-1

您還可以使用HTML標籤,如:

JButton button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>"); 

或以任何其他的JComponent像你一樣。

因此,您可以使用<BR>標籤來實現您的需要。

+0

這不是OP要求的內容。 –

相關問題