import javax.swing.*;
public class JFrameDemo extends JFrame {
public JFrameDemo(boolean b) {
this.setTitle("自定義窗體");
this.setBounds(80,80,300,200);
this.setResizable(b);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame frame = new JFrameDemo(false);
}
}
【例8.4】創(chuàng)建JTextArea文本域組件,并將其放入滾動面板JScrollPane中,最后將滾動面板加入窗體容器中。import javax.swing.*;
public class JScrollPaneDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("JScrollPane應(yīng)用實(shí)例");
JTextArea ta = new JTextArea("帶滾動條的面板演示實(shí)例?。?!");
JScrollPane sp = new JScrollPane(ta);
jf.add(sp);
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.5】創(chuàng)建水平方向分隔面板outerPane和垂直方向分隔面板interPane,并將inter-Pane設(shè)置為outerPane面板的右邊部分,同時(shí)設(shè)置分隔條的初始位置和大小。import javax.swing.*;
public class JSplitPaneDemo extends JFrame{
private JSplitPane outerPane,interPane;
public JSplitPaneDemo(){
outerPane =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true);
outerPane.setOneTouchExpandable(true);
interPane =new JSplitPane(JSplitPane.VERTICAL_SPLIT);
interPane.setOneTouchExpandable(true);
interPane.setLeftComponent(new JLabel("右上窗格"));
interPane.setRightComponent(new JLabel("右下窗格"));
outerPane.setLeftComponent(new JLabel("左邊窗格"));
outerPane.setRightComponent(interPane);
outerPane.setDividerLocation(60);
interPane.setDividerLocation(70);
outerPane.setDividerSize(10);
interPane.setDividerSize(10);
this.add(outerPane);
this.setTitle("分隔面板應(yīng)用");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args)
{
new JSplitPaneDemo();
}
}
【例8.6】使用FlowLayout布局管理器布局JFrame組件中的5個(gè)按鈕。import java.awt.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("流布局管理器");
FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
flowLayout.setHgap(10);
flowLayout.setVgap(20);
frame.setLayout(flowLayout);
for(int i = 1;i<= 5;i++) {
frame.add(new JButton("Button" + i));
frame.setSize(400,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
【例8.7】按照BorderLayout布局管理器在JFrame窗體中放置5個(gè)按鈕。import java.awt.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame {
public BorderLayoutDemo() {
super("BorderLayout布局管理器");
BorderLayout layout = new BorderLayout(5,5);
this.setLayout(layout);
JButton buttonEast = new JButton("東");
JButton buttonWest = new JButton("西");
JButton buttonSouth = new JButton("南");
JButton buttonNorth = new JButton("北");
JButton buttonCenter = new JButton("中");
this.add(buttonNorth , BorderLayout.NORTH);
this.add(buttonSouth , BorderLayout.SOUTH);
this.add(buttonEast , BorderLayout.EAST);
this.add(buttonWest , BorderLayout.WEST);
this.add(buttonCenter , BorderLayout.CENTER);
this.setSize(300 , 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
BorderLayoutDemo application = new BorderLayoutDemo();
}
}
【例8.8】按GridLayout布局管理器部署JFrame窗體中6個(gè)按鈕。import java.awt.*;
import javax.swing.*;
public class GridLayoutDemo extends JFrame {
private final String names[] = {"one" , "two" , "three" ,
"four" , "five" , "six"};
public GridLayoutDemo() {
super("GridLayout布局管理器");
GridLayout grid1 = new GridLayout(2,2,5,5);
this.setLayout(grid1);
JButton buttons[] = new JButton[names.length];
for(int count = 0;count< names.length;count++) {
buttons[count] = new JButton(names[count]);
this.add(buttons[count]);
}
this.setSize(300,150);
this.setVisible(true);
}
public static void main(String args[]) {
GridLayoutDemo application = new GridLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.9】在JFrame中創(chuàng)建cardPanel和controlPanel兩個(gè)JPanel容器,并使用默認(rèn)布局管理器將它們分別部署到“Center”和“South”區(qū)域。cardPanel容器的布局管理器設(shè)置為CardLayout,部署4個(gè)按鈕組件;controlPanel容器的布局管理器采用默認(rèn),部署兩個(gè)按鈕組件。import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutDemo extends JFrame {
private CardLayout cardLayout;
private JPanel cardPanel;
public CardLayoutDemo() {
super("CardLayout布局管理器");
cardPanel = new JPanel();
cardLayout = new CardLayout(10,10);
cardPanel.setLayout(cardLayout);
for(int i = 1;i<= 4;i++) {
cardPanel.add("button" + i , new JButton("button" + i));
}
this.add(cardPanel , BorderLayout.CENTER);
JButton nextButton = new JButton("Next");
JButton prevButton = new JButton("Previous");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(cardPanel);
}
});
prevButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(cardPanel);
}
});
JPanel controlPanel = new JPanel();
controlPanel.add(prevButton);
controlPanel.add(nextButton);
this.add(controlPanel , BorderLayout.SOUTH);
}
public static void main(String[] args) {
CardLayoutDemo cardDemo = new CardLayoutDemo();
cardDemo.setSize(300,200);
cardDemo.setVisible(true);
cardDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.12】本例以窗口JFrame作為事件源演示W(wǎng)indowEvent事件處理。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowEventDemo extends JFrame implements WindowConstants{
public WindowEventDemo(){
super("WindowEvent處理應(yīng)用");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
public void windowActivated(WindowEvent e){
System.out.println("This window is activated.");
}
public void windowClosed(WindowEvent e){
System.out.println("This window has closed.");
System.exit(0);
}
public void windowClosing(WindowEvent e) {
System.out.println("This window is closing now.");
this.dispose();
}
public void windowDeactivated(WindowEvent e){
System.out.println("This window is deactivated");
}
public void windowDeiconified(WindowEvent e){
System.out.println("This window is deiconified");
}
public void windowIconified(WindowEvent e){
System.out.println("This window is iconified");
}
public void windowOpened(WindowEvent e){
System.out.println("This window is opened");
}
public static void main(String[] args){
new WindowEventDemo();
}
}
【例8.13】以JTextArea組件作為產(chǎn)生KeyEvent事件的事件源的鍵盤事件處理程序。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyEventDemo extends JFrame implements KeyListener {
private JTextField tf;
public KeyEventDemo(){
super("KeyEvent處理應(yīng)用");
tf=new JTextField(15);
tf.addKeyListener(this);
JPanel panel=new JPanel();
panel.add(new JLabel("輸入框"));
panel.add(tf);
this.add(panel,BorderLayout.CENTER);
this.setSize(300,80);
this.setVisible(true);
}
public void keyPressed(KeyEvent e){}
public void keyRelesased(KeyEvent e){}
public void keyTyped(KeyEvent e){
System.out.println(e.getKeyChar());
}
public static void main(String[] args){
new KeyEventDemo();
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
創(chuàng)新互聯(lián)建站主營城陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),城陽h5微信小程序搭建,城陽網(wǎng)站營銷推廣歡迎城陽等地區(qū)企業(yè)咨詢