本文主要是介绍Java图形用户界面(GUI)工具包swing来写一个简单的留言板程序(不带数据库),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java图形用户界面(GUI)工具包swing来写一个简单的留言板程序(不带数据库)
package demo2;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.util.Date;public class jiang1 extends JFrame {private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private JLabel jLabel = null;private JTextArea jTextArea = null;private JScrollPane jScrollPane = null;private JLabel jLabel1 = null;private JComboBox jComboBox = null;private JLabel jLabel2 = null;private JTextField jTextField = null;private JButton jButton = null;private JButton jButton1 = null;private JButton jButton2 = null;private JButton jButton3 = null;/*** This is the default constructor*/public jiang1() {super();initialize();}/*** This method initializes this* * @return void*/private void initialize() {this.setContentPane(getJContentPane());this.setTitle("留言板程序");this.setBounds(new Rectangle(0, 0, 640, 680));this.setVisible(true);}/*** This method initializes jContentPane* * @return javax.swing.JPanel*/private JPanel getJContentPane() {if (jContentPane == null) {jLabel2 = new JLabel();jLabel2.setBounds(new Rectangle(210, 407, 90, 24));jLabel2.setFont(new Font("Dialog", Font.BOLD, 14));jLabel2.setText("输入留言:");jLabel1 = new JLabel();jLabel1.setBounds(new Rectangle(40, 407, 90, 24));jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));jLabel1.setText("选择角色:");jLabel = new JLabel();jLabel.setBounds(new Rectangle(283, 15, 45, 20));jLabel.setHorizontalAlignment(SwingConstants.CENTER);jLabel.setFont(new Font("Dialog", Font.BOLD, 14));jLabel.setText("留言板");jContentPane = new JPanel();jContentPane.setLayout(null);jContentPane.add(jLabel, null);jContentPane.add(getJScrollPane(), null);jContentPane.add(jLabel1, null);jContentPane.add(getJComboBox(), null);jContentPane.add(jLabel2, null);jContentPane.add(getJTextField(), null);jContentPane.add(getJButton(), null);jContentPane.add(getJButton1(), null);jContentPane.add(getJButton2(), null);jContentPane.add(getJButton3(), null);}return jContentPane;}/*** This method initializes jTextArea * * @return javax.swing.JTextArea */private JTextArea getJTextArea() {if (jTextArea == null) {jTextArea = new JTextArea();jTextArea.setText("留言内容:");jTextArea.setEditable(false);}return jTextArea;}/*** This method initializes jScrollPane * * @return javax.swing.JScrollPane */private JScrollPane getJScrollPane() {if (jScrollPane == null) {jScrollPane = new JScrollPane();jScrollPane.setBounds(new Rectangle(22, 49, 534, 347));jScrollPane.setViewportView(getJTextArea());}return jScrollPane;}/*** This method initializes jComboBox * * @return javax.swing.JComboBox */private JComboBox getJComboBox() {if (jComboBox == null) {jComboBox = new JComboBox();jComboBox.setBounds(new Rectangle(110, 407, 100, 24));String[] mycbox={"管理员","博主","游客"};jComboBox.addItem(mycbox[0]);jComboBox.addItem(mycbox[1]);jComboBox.addItem(mycbox[2]);}return jComboBox;}/*** This method initializes jTextField * * @return javax.swing.JTextField */private JTextField getJTextField() {if (jTextField == null) {jTextField = new JTextField();jTextField.setBounds(new Rectangle(280, 407, 231, 200));}return jTextField;}/*** This method initializes jButton * * @return javax.swing.JButton */private JButton getJButton() {if (jButton == null) {jButton = new JButton();jButton.setBounds(new Rectangle(505, 407, 70, 24));jButton.setText("发帖");jButton.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e) {Date date = new Date();jTextArea.setText(jTextArea.getText()+"\r\n"+date.toString()+ " " + jComboBox.getSelectedItem().toString()+" 留言:"+jTextField.getText());}});}return jButton;}/*** This method initializes jButton1 * * @return javax.swing.JButton */private JButton getJButton1() {if (jButton1 == null) {jButton1 = new JButton();jButton1.setBounds(new Rectangle(565, 51, 60, 32));jButton1.setText("删帖");jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e) {// jTextArea.setText("留言内容:");// if(jComboBox.getSelectedItem().toString() == "管理员" || jComboBox.getSelectedItem().toString() =="博主" ){}});}return jButton1;}/*** This method initializes jButton2 * * @return javax.swing.JButton */private JButton getJButton2() {if (jButton2 == null) {jButton2 = new JButton();jButton2.setBounds(new Rectangle(560, 112, 80, 32));jButton2.setText("从头浏览");jButton2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e) {jTextArea.setCaretPosition(0);}});}return jButton2;}/*** This method initializes jButton3 * * @return javax.swing.JButton */private JButton getJButton3() {if (jButton3 == null) {jButton3 = new JButton();jButton3.setBounds(new Rectangle(560, 173, 80, 32));jButton3.setText("从尾浏览");jButton3.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e) {jTextArea.setCaretPosition((int)jTextArea.getText().length());}});}return jButton3;}public static void main(String args[]){new jiang1();}}
这篇关于Java图形用户界面(GUI)工具包swing来写一个简单的留言板程序(不带数据库)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!