生成一个为Swing添加组件的类有一个addZJ(JComponent j,int x,int y,String text )方法
时间: 2024-02-29 14:51:33 浏览: 14
好的,这是一个为Swing添加组件的类,其中包含一个addZJ方法:
```
import javax.swing.*;
public class SwingAddComponent {
private JFrame frame;
public SwingAddComponent() {
frame = new JFrame("Swing Add Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(null);
frame.setVisible(true);
}
public void addZJ(JComponent j, int x, int y, String text) {
j.setBounds(x, y, 100, 30);
j.setToolTipText(text);
frame.add(j);
}
public static void main(String[] args) {
SwingAddComponent swingAddComponent = new SwingAddComponent();
JLabel label = new JLabel("Label");
swingAddComponent.addZJ(label, 50, 50, "This is a label.");
JButton button = new JButton("Button");
swingAddComponent.addZJ(button, 200, 50, "This is a button.");
}
}
```
在这个类中,addZJ方法接收三个参数:要添加的组件j,组件的x和y坐标,以及组件的提示文本text。方法中会设置组件的位置和提示文本,并将组件添加到窗口中。在main方法中,我们使用这个类来添加了一个JLabel和一个JButton。