我正在做一個簡單的程式,JTextArea在單擊 JButton 后顯示有關特定大學的資訊。
單擊按鈕后如何使 JTextArea 出現?
這是我的代碼:
package toolBar;
import javax.swing.*;
import java.awt.*;
public class ToolBar extends JFrame {
JFrame frame=new JFrame();
JButton uni1=new JButton("Hasheimte University");
JButton uni2=new JButton("The University of Jordan");
JButton uni3=new JButton("German Jordanian University");
JButton exit=new JButton("Close");
JToolBar tb = new JToolBar();
JTextArea text = new JTextArea("bla bla bla");
public ToolBar(){
setTitle("Jordanian universities");
setSize(600,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(tb);
tb.add(uni1);
tb.add(uni2);
tb.add(uni3);
tb.add(exit);
tb.setFloatable(false);
setLayout(new FlowLayout (FlowLayout.CENTER));
uni1.addActionListener(e ->{
});
exit.addActionListener(e -> {
dispose();
});
}
}
uj5u.com熱心網友回復:
所有 Swing 組件都有一個setVisible您可以使用的方法。您從一個不可見的組件開始,然后單擊按鈕使其可見。
另一種方法是通過單擊按鈕 ( Container#add)將其添加到父容器,但這需要重新驗證布局。因此,第一個選項更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349499.html
