📄 bloggui.java
字号:
package applet;
/**
* This example shows how we can reduce the previous example (ex1) to one class.
*/
import javax.swing.*;
import java.awt.*; // required for Dimension
public class BlogGUI extends JPanel {
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
private static final long serialVersionUID = 8016418091174982986L;
private JFrame frame;
private String title = "My Blog";
public BlogGUI() {
setOpaque(true);
/* Name label and text field */
JLabel nameLabel = new JLabel("Name:");
int nameFieldLength = 10;
JTextField nameTextField = new JTextField(nameFieldLength);
add(nameLabel);
add(nameTextField);
/* Age label and text field */
JLabel dateLabel = new JLabel("Date: ");
int dateFieldLength = 5;
JTextField dateTextField = new JTextField(dateFieldLength);
/* Adding the elements to the panel */
add(dateLabel);
add(dateTextField);
/* Text Area to type info */
JTextArea textArea = new JTextArea();
textArea.setEditable(true);
/* Adding scroll bar capabilities to output area */
/* NOTE: Scroll bars will appear after typing several lines */
JScrollPane scrollPane = new JScrollPane(textArea);
int width = 350, height = 200;
scrollPane.setPreferredSize(new Dimension(width, height));
/* Setting the size of the scroll area */
/* Adding the scrollPane to the panel */
add(scrollPane);
/* Adding a check box */
JCheckBox familyCheckBox = new JCheckBox("Family");
add(familyCheckBox);
JCheckBox allCheckBox = new JCheckBox("All");
add(allCheckBox);
/* Adding Post button */
JButton postButton = new JButton("Post");
add(postButton);
}
public void defineFrame() {
/* Frame to hold the panel */
frame = new JFrame(title);
frame.setContentPane(this); // adds the panel
frame.setSize(new Dimension(WIDTH, HEIGHT));
/* Some work to centralize the frame */
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Upper left corner
int upperLeftCornerX = (screenSize.width - frame.getWidth()) / 2;
int upperLeftCornerY = (screenSize.height - frame.getHeight()) / 2;
frame.setLocation(upperLeftCornerX, upperLeftCornerY);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BlogGUI().defineFrame();
}
});
}
/* Static block for feel and look. Comment this */
/* block out to see how the feel and look changes. */
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -