实验九 图形用户界面

(1.使用布局实现网络小说阅读器的界面

要求

1. 编写如下图所示的界面。其中显示小说的地方使用JTextArea类,按钮使用JButton,搜索文本框使用JTextField,下拉列表框使用JComboBox。布局和下图一模一样

样例图
运行结果图


xiaoshuobuju.java

package shiyan9_1;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.Insets;

import javax.swing.*;

public class xiaoshuobuju {

public static void main (String[] args){

  JFrameframe = new JFrame();

  ContainercontentPane=frame.getContentPane();

  contentPane.setLayout(newGridLayout(1,2));

  frame.setLayout(null);//如果你不加这么句setLayout(null);那么这两个按键就会使用默认布局,即使你使用btn.setBounds();来控制他们的位置也没用。

  frame.setTitle("布局");

  frame.setSize(510, 250);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.setVisible(true);

  frame.setLocation(200, 200);//设置窗口的位置,默认位置是(0,0)。

  //frame.setResizable(false);//设置窗口是否可调整大小

  JButtonbtn1 = new JButton("统计字数");

  JButtonbtn2 = new JButton("标记位置");

  JButtonbtn3 = new JButton("跳转");

  JButtonbtn4 = new JButton("查找");

  btn1.setMargin(newInsets(0,0,0,0));//设置按钮边框和标签之间的空白

  btn2.setMargin(newInsets(0,0,0,0));

  btn3.setMargin(newInsets(0,0,0,0));

  btn4.setMargin(newInsets(0,0,0,0));

       btn1.setBounds(390, 10, 70, 30);

       btn2.setBounds(390, 40, 70, 30);

       btn3.setBounds(400, 135, 40, 30);

       btn4.setBounds(210, 175, 40, 20);

  btn1.setVisible(true);

  btn2.setVisible(true);

  btn3.setVisible(true);

  btn4.setVisible(true);

  frame.add(btn1);

  frame.add(btn2);

  frame.add(btn3);

  frame.add(btn4);

  JComboBoxfaceCombo;

  faceCombo = newJComboBox<String>();

  faceCombo.addItem("第一页");

  faceCombo.addItem("第二页");

  frame.add(faceCombo);

  faceCombo.setEditable(true);

  faceCombo.setEditable(false);

  faceCombo.setBounds(390, 100, 70, 30);//这个对号放在后面一点

  JTextAreatextArea = new JTextArea("唧唧复唧唧,木兰当户织。不闻机杼声,惟闻女叹息。问女何所思,问女何所忆。女亦无所思,女亦无所忆。昨夜见军帖,可汗大点兵,军书十二卷,卷卷有爷名。阿爷无大儿,木兰无长兄,愿为市鞍马,从此替爷征。东市买骏马,西市买鞍鞯,南市买辔头,北市买长鞭。旦辞爷娘去,暮宿黄河边,不闻爷娘唤女声,但闻黄河流水鸣溅溅。旦辞黄河去,暮至黑山头,不闻爷娘唤女声,但闻燕山胡骑鸣啾啾。万里赴戎机,关山度若飞。朔气传金柝,寒光照铁衣。将军百战死,壮士十年归。归来见天子,天子坐明堂。策勋十二转,赏赐百千强。可汗问所欲,木兰不用尚书郎,愿驰千里足,送儿还故乡。");

  textArea.setLineWrap(true);

  frame.add(textArea);

  textArea.setVisible(true);

  textArea.setBounds(10, 10, 370, 160);

  JLabellabel01 = new JLabel();

  label01.setText("要查找的文字:");

  frame.add(label01);

  label01.setVisible(true);

  label01.setBounds(20, 170, 100, 30);

  JTextFieldjTextField = new JTextField();

  jTextField.setVisible(true);

  frame.add(jTextField);

  jTextField.setBounds(105, 175, 100, 20);

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容