我想在点击按钮CONNECT_BUTTON后显示我的JTextArea。我该怎么办 ?此外,我正在尝试在我的JTextArea中添加一些网格,因为我想用它来显示数据库中的一些记录。任何想法如何做到这一点?
package DataBase_Hospital;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test extends JFrame implements ActionListener {
private JButton FIND_BUTTON;
private JButton MESSAGE_BUTTON;
private JButton CONNECT_BUTTON;
private JButton CLEAR_BUTTON;
private JButton ADD_BUTTON;
private JButton RAPORT_BUTTON;
private JButton EDIT_BUTTON;
private JButton DOWNLOAD_BUTTON;
private JTextArea DATABASE_FIELD;
private DatabaseManagement DATABASE;
public test(){
setTitle("Hospital Management");
setSize(900,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JLabel background=new JLabel(new ImageIcon("/Users/Dominik/Desktop/j.jpg"));
add(background);
DATABASE_FIELD = new JTextArea();
JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
scrollPane.setBounds(50, 50, 800, 400);
background.add(scrollPane);
DATABASE_FIELD.setEditable(true);
DATABASE_FIELD.setLayout(new GridLayout(3, 3));
//DATABASE_FIELD.setVisible(false);
CONNECT_BUTTON = new JButton();
CONNECT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/connect_no.png"));
CONNECT_BUTTON.setBounds(165, 500, 60, 60);
background.add(CONNECT_BUTTON);
CONNECT_BUTTON.addActionListener(this);
CONNECT_BUTTON.setToolTipText("Connect to Data Base");
FIND_BUTTON = new JButton();
FIND_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/search.png"));
FIND_BUTTON.setBounds(240, 500, 60, 60);
background.add(FIND_BUTTON);
FIND_BUTTON.addActionListener(this);
FIND_BUTTON.setToolTipText("Find record in Data Base");
ADD_BUTTON = new JButton();
ADD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_add.png"));
ADD_BUTTON.setBounds(315, 500, 60, 60);
background.add(ADD_BUTTON);
ADD_BUTTON.addActionListener(this);
ADD_BUTTON.setToolTipText("Add record to Data Base");
RAPORT_BUTTON = new JButton();
RAPORT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/raport.png"));
RAPORT_BUTTON.setBounds(392, 500, 60, 60);
background.add(RAPORT_BUTTON);
RAPORT_BUTTON.addActionListener(this);
RAPORT_BUTTON.setToolTipText("Generates raport");
EDIT_BUTTON = new JButton();
EDIT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_edit.png"));
EDIT_BUTTON.setBounds(467, 500, 60, 60);
background.add(EDIT_BUTTON);
EDIT_BUTTON.addActionListener(this);
EDIT_BUTTON.setToolTipText("Edit record from Data Base");
CLEAR_BUTTON = new JButton();
CLEAR_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/delete.png"));
CLEAR_BUTTON.setBounds(544, 500, 60, 60);
background.add(CLEAR_BUTTON);
CLEAR_BUTTON.addActionListener(this);
CLEAR_BUTTON.setToolTipText("Clear all Data Base");
MESSAGE_BUTTON = new JButton();
MESSAGE_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/message.png"));
MESSAGE_BUTTON.setBounds(619, 500, 60, 60);
background.add(MESSAGE_BUTTON);
MESSAGE_BUTTON.addActionListener(this);
MESSAGE_BUTTON.setToolTipText("Send message to another user");
DOWNLOAD_BUTTON = new JButton();
DOWNLOAD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/download.png"));
DOWNLOAD_BUTTON.setBounds(694, 500, 60, 60);
background.add(DOWNLOAD_BUTTON);
DOWNLOAD_BUTTON.addActionListener(this);
DOWNLOAD_BUTTON.setToolTipText("Download Data Base to a text file");
validate();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test window = new test();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object EVENT_SOURCE = e.getSource();
DATABASE = new DatabaseManagement();
if (EVENT_SOURCE == CLEAR_BUTTON)
{
System.out.println("siema");
}
else if (EVENT_SOURCE == DOWNLOAD_BUTTON)
{
dispose();
}
else if (EVENT_SOURCE == CONNECT_BUTTON)
{
DATABASE_FIELD.setText("");
//** TRZEBA ZROBIC SIATKE !!! **//
DATABASE_FIELD.append("IMIE ");
DATABASE_FIELD.append("NAZWISKO ");
DATABASE_FIELD.append("PESEL \n");
DATABASE_FIELD.append(DATABASE.showDataBase());
}
}
}
答案 0 :(得分:2)
如果您要将LayoutManager
用作JLabel
(网格或FlowLayout),则必须设置正确的container
不要使用NullLayout
JTextArea
未指定为容器移除DATABASE_FIELD.setLayout(new GridLayout(3, 3));
使用JTextArea (10, 15)
作为初始大小而不是任何大小调整(然后对JScrollPane
也有效)
将JScrollPane
与JTextArea
添加到JFrames CENTER area
或将JLabel
的LayoutManager更改为BorderLayout
,然后将JButtons
添加到JLabel
另一个单独的setTitle("Hospital Management");
setSize(900,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
(网格或FlowLayout)
pack()
将这些代码行移动到构造函数的末尾,然后删除validate,另外应该是revalidate()和repaint(),因为你没有为图像重新绘制而停止
致电setSize
而非{{1}}
只有一个可能的问题,只是最重要的事情