本文介紹了未顯示JTextArea的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
為什么JTextArea
沒(méi)有顯示在GUI
中?
public class AddMovie extends JTextField {
static JFrame frame;
private JLabel description;
JTextArea movieDescription;
public JPanel createContentPane() throws IOException
{
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
totalGUI.setBackground(Color.WHITE);
description = new JLabel("Description ");
description.setLocation(15,285);
description.setSize(120, 25);
description.setFont(new java.awt.Font("Tahoma", 0, 12));
movieDescription=new JTextArea();
movieDescription.setLocation(15,320);
movieDescription.setSize(420, 110);
JScrollPane scrollPane = new JScrollPane(movieDescription);
totalGUI.add(description);
totalGUI.add(movieDescription);
totalGUI.add(cancel);
totalGUI.add(scrollPane);
return totalGUI;
}
static void createAndShowGUI() throws IOException
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("New Movie");
//Create and set up the content pane.
AddMovie demo = new AddMovie();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(515, 520);
frame.setLocation(480,120);
frame.setVisible(true);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}
推薦答案
如here和here所示,null
布局會(huì)招致麻煩。因?yàn)槟娘@示以描述為中心,所以使用JTextArea
構(gòu)造函數(shù),該構(gòu)造函數(shù)允許您以行和列指定大小。當(dāng)您pack()
包圍的Window
時(shí),它將調(diào)整大小以適應(yīng)文本區(qū)域。我添加了一些臨時(shí)文本來(lái)說(shuō)明效果。我還編寫(xiě)了updated允許文本區(qū)域在調(diào)整框架大小時(shí)增長(zhǎng)的代碼。
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* @see https://stackoverflow.com/a/38282886/230513
*/
public class Test {
private JPanel createPanel() {
JPanel panel = new JPanel(new GridLayout());
//panel.setBorder(BorderFactory.createTitledBorder("Description"));
JTextArea movieDescription = new JTextArea(10, 20);
panel.add(new JScrollPane(movieDescription));
movieDescription.setLineWrap(true);
movieDescription.setWrapStyleWord(true);
movieDescription.setText(movieDescription.toString());
return panel;
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(createPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
這篇關(guān)于未顯示JTextArea的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,