1.编写程序对常用的布局管理进行演示,参考界面如图所示。 2.编写程序演示菜单、对话框的实现,参考界面如图所示
时间: 2024-05-04 22:16:05 浏览: 158
很高兴为您解答问题。以下是我的回答:
1. 对于常用的布局管理,可以使用Java Swing或JavaFX来实现。以Java Swing为例,可以使用GridLayout、BorderLayout、BoxLayout等布局管理器,通过设置不同的参数来达到不同的布局效果。具体代码实现可以参考如下:
```java
import java.awt.*;
import javax.swing.*;
public class LayoutDemo extends JFrame {
public LayoutDemo() {
setTitle("布局演示");
setLayout(new BorderLayout());
JPanel panel1 = new JPanel(new GridLayout(2, 2));
panel1.add(new JButton("按钮1"));
panel1.add(new JButton("按钮2"));
panel1.add(new JButton("按钮3"));
panel1.add(new JButton("按钮4"));
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(new JLabel("标签1"));
panel2.add(new JTextField("文本框1"));
panel2.add(new JButton("按钮5"));
JPanel panel3 = new JPanel(new BoxLayout(new JPanel(), BoxLayout.Y_AXIS));
panel3.add(new JLabel("标签2"));
panel3.add(new JTextField("文本框2"));
panel3.add(new JCheckBox("复选框1"));
JPanel panel4 = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 10, 10);
panel4.add(new JLabel("标签3"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel4.add(new JTextField("文本框3"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel4.add(new JRadioButton("单选按钮1"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel4.add(new JRadioButton("单选按钮2"), gbc);
JPanel panel5 = new JPanel(new BorderLayout());
panel5.add(new JButton("按钮6"), BorderLayout.NORTH);
panel5.add(new JButton("按钮7"), BorderLayout.SOUTH);
panel5.add(new JButton("按钮8"), BorderLayout.WEST);
panel5.add(new JButton("按钮9"), BorderLayout.EAST);
panel5.add(new JTextArea(), BorderLayout.CENTER);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.WEST);
add(panel3, BorderLayout.CENTER);
add(panel4, BorderLayout.EAST);
add(panel5, BorderLayout.SOUTH);
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new LayoutDemo();
}
}
```
2. 对于菜单和对话框的实现,可以使用Java Swing的JMenuBar、JMenu、JMenuItem来实现菜单,使用JDialog来实现对话框。具体代码实现可以参考如下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuDemo extends JFrame {
public MenuDemo() {
setTitle("菜单和对话框演示");
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("文件");
menuBar.add(fileMenu);
JMenuItem openItem = new JMenuItem("打开");
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(MenuDemo.this);
if (result == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile().getPath());
}
}
});
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("保存");
saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(MenuDemo.this);
if (result == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile().getPath());
}
}
});
fileMenu.add(saveItem);
JMenu editMenu = new JMenu("编辑");
menuBar.add(editMenu);
JMenuItem cutItem = new JMenuItem("剪切");
editMenu.add(cutItem);
JMenuItem copyItem = new JMenuItem("复制");
editMenu.add(copyItem);
JMenuItem pasteItem = new JMenuItem("粘贴");
editMenu.add(pasteItem);
JButton showDialogButton = new JButton("显示对话框");
showDialogButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MyDialog dialog = new MyDialog(MenuDemo.this, "对话框演示", true);
dialog.setVisible(true);
}
});
JPanel panel = new JPanel(new FlowLayout());
panel.add(showDialogButton);
add(panel);
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MenuDemo();
}
}
class MyDialog extends JDialog {
public MyDialog(JFrame owner, String title, boolean modal) {
super(owner, title, modal);
JTextField nameField = new JTextField(20);
JTextField telField = new JTextField(20);
JPanel panel = new JPanel(new GridLayout(2, 2));
panel.add(new JLabel("姓名:"));
panel.add(nameField);
panel.add(new JLabel("电话:"));
panel.add(telField);
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("姓名:" + nameField.getText());
System.out.println("电话:" + telField.getText());
setVisible(false);
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
add(panel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(owner);
}
}
```
阅读全文