大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package hao;
创新互联-专业网站定制、快速模板网站建设、高性价比阜新网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式阜新网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖阜新地区。费用合理售后完善,十年实体公司更值得信赖。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;
JButton music;
public ChatPanel() {
setLayout(new BorderLayout());
text = new JTextPane();
text.setEditable(false);
doc = text.getStyledDocument();//跟踪文本和图片写到该区域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面对JComboBox的巧用***********************************************************************
String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);
fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//创建一个可以容纳多个Box组件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字体~~"), b2 = new JLabel("样式~~"),b3 = new JLabel("字号~~"),b4 = new JLabel("颜色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);
textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));
inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //设置文本区的换行策略。88888*********************************
send = new JButton("发送");
record=new JButton("显示记录");
saveRecord=new JButton("储存记录");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //设置按钮间距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置Box的边框线********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);
inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置输入窗口边框线*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//设置输入窗口边框线*******************
JPanel audioPanel = new JPanel();//最上面的边框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));
music = new JButton("想听就听");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//设置输入窗口边框线*******************
audioPanel.add(music);
add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}
void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}
public void insert(MessageStyle attrib) {
try {
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//写完后接着换行************
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public MessageStyle getMessageStyle(String line) {
MessageStyle att = new MessageStyle();
att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗体")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜体")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜体")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}
String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("红色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("蓝色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黄色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("绿色")) {
att.setColor(new Color(0, 255, 0));
}
String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡红")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡蓝")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黄")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡绿")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}
}
首先:采用什么技术实现
java语言可以使用awt 和swing等技术实现图形界面
推荐使用Swing,因为Swing比AWT更专业,更漂亮,组件更丰富,功能更强大。
2. 其次:分析采用什么布局
边界布局BorderLayout,配合表格布局GridLayout,既简单又美观
3. 最后:分析需求中需要用的组件
学生姓名 学号 显示信息 需要用到文本框JTextField
单选按钮 需要用到组件 JRadioButton
复选框 需要用到组件 JCheckBox
组合框 需要用到组件 JComboBox
图片效果
参考代码如下
//导入所需要的包
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.*;
public class ClassFrame extends JFrame {// 写一个类继承自JFrame 窗体
// 定义组件
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfName, tfNum, allInfo;
private JRadioButton rb1, rb2;
private JCheckBox cb1, cb2, cb3;
private JComboBoxString t1, t2, t3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClassFrame frame = new ClassFrame();// 创建一个窗口实例
frame.setVisible(true);// 让该窗口实例可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 窗口属性的设置,内部组件的初始化
*/
public ClassFrame() {
setTitle("选课ing...");//标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭是退出JVM
setSize(450, 339);// 设置窗体大小
setLocationRelativeTo(null);// 窗体居中
contentPane = new JPanel();// 内容面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));// 设置布局
setContentPane(contentPane);
JPanel panel = new JPanel(new GridLayout(5, 1, 5, 10));//5行1列的表格布局
panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
contentPane.add(panel, BorderLayout.CENTER);//给panel添加边框
JPanel panel_1 = new JPanel();
panel.add(panel_1);
JLabel label = new JLabel("姓名");
panel_1.add(label);
tfName = new JTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabel label_2 = new JLabel("学号");
panel_1.add(label_2);
tfNum = new JTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1 = new JRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//设置单选按钮中,默认选择的按钮
rb2 = new JRadioButton("女");
panel_1.add(rb2);
ButtonGroup bts = new ButtonGroup();//单选按钮需要加入同一个ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanel panel_2 = new JPanel();
panel.add(panel_2);
cb1 = new JCheckBox("高等数学");
panel_2.add(cb1);
t1 = new JComboBoxString();
t1.setModel(new DefaultComboBoxModelString(new String[] { "林老师", "赵老师", "孙老师" }));
panel_2.add(t1);
JPanel panel_3 = new JPanel();
panel.add(panel_3);
cb2 = new JCheckBox("世界经济");
panel_3.add(cb2);
t2 = new JComboBoxString();
t2.setModel(new DefaultComboBoxModelString(new String[] { "张老师", "刘老师" }));
panel_3.add(t2);
JPanel panel_4 = new JPanel();
panel.add(panel_4);
cb3 = new JCheckBox("音乐赏析");
panel_4.add(cb3);
t3 = new JComboBoxString();
t3.setModel(new DefaultComboBoxModelString(new String[] { "王老师", "周老师" }));
panel_4.add(t3);
JPanel panel_5 = new JPanel();
panel.add(panel_5);
JButton jbOk = new JButton("确定");
panel_5.add(jbOk);
JButton jbRest = new JButton("重填");
panel_5.add(jbRest);
JPanel panelSouth = new JPanel();
contentPane.add(panelSouth, BorderLayout.SOUTH);
JLabel labe = new JLabel("选课信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo = new JTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanel panelNorth = new JPanel();
contentPane.add(panelNorth, BorderLayout.NORTH);
JLabel labelTitle = new JLabel("学生选课界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(new Font("宋体", Font.BOLD, 20));
panelNorth.add(labelTitle);
//给确定按钮添加事件处理代码
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuilder info = new StringBuilder();
String name = tfName.getText();
String num = tfNum.getText();
String sex;
if (rb1.isSelected()) {
sex = "男";
} else {
sex = "女";
}
info.append(name + num + sex);
if (cb1.isSelected()) {
String c = cb1.getText();
String t = t1.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb2.isSelected()) {
String c = cb2.getText();
String t = t2.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb3.isSelected()) {
String c = cb3.getText();
String t = t3.getSelectedItem().toString();
info.append(" " + c + t);
}
allInfo.setText(info.toString());//把学生信息和选课信息放到文本框
}
});
//给重填按钮 设置事件处理代码
jbRest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
帮你改了一下,看注释的地方。
import java.awt.*;
import java.awt.event.*;
public class MoveWord extends Frame
{
private TextArea eastArea = new TextArea( 7, 20 );
private TextArea westArea = new TextArea( 7, 20 );
private Button toLeft = new Button( "-" );
private Button toRight = new Button( "-" );
public MoveWord()
{
super( "MoveWord" );
this.setLayout( new FlowLayout() );
this.add( westArea );
Panel pal = new Panel();
pal.setLayout( new GridLayout( 2, 1, 10, 10 ) );
pal.add( toLeft );
pal.add( toRight );
toLeft.addActionListener( new Handler() );
toRight.addActionListener( new Handler() );
this.add( pal );
this.add( eastArea );
/*addWindowListener( new WindowAdapter() )
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}*/
//上面/*……*/部分改为
addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );//匿名内部类的形式
setSize( 400, 200 );
setVisible( true );
}
class Handler implements ActoinListener //ActoinListener改为ActionListener
{
public void actionPerformed( ActionEvent e )
{
String copyText = "";
if( e.getSource() == toLeft )
{
copyText = eastArea.getSelectedText();
westArea.append( copyText );
}
else
{
copyText = westArea.getSelectedText();
eastArea.append( copyText );
}
}
}
public static void main( string args[] ) //string改为String
{
MoveWord word = new MoveWord();
}
}
一: 首先弄清题目的意思
A.需要的主要组件列表:
1. 创建一个窗口,窗口标题叫Information
2. 3个标签, 用于显示文字 Name Number Class
3. 3个文本框, 用于填写信息
4. 1个按钮, 文字是确认
5. 1个文本域
B.业务逻辑
1. 当点击按钮确认的时候, 把 文本框的信息显示到文本域
C.设计的主要技术
JLabel , JButton, JTextField ...等, 都是swing的组件 , 所以应该使用swing进行创建
二: 确定使用的布局
swing虽然重写了大部分的组件, 但是布局, 依旧沿袭awt技术
分析图片上的布局:
至少有2种方法可以实现,
方法一: 绝对布局 , 优点: 配合可视化GUI拖曳, 可以完美的实现图上的组件的位置
但是缺点也是致命的, 不同的操作系统平台下, 可能会出现位置的移动,
只适合开发平台, 移植效果差 . 所以不推荐使用
方法二: 灵活的表格布局, 配合流式布局 , 所有操作系统下,显示效果都比较统一.
三: 效果图
四: 参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame {
//申明需要的组件
private final JTextField jtf1,jtf2,jtf3;
private final JTextArea jta;
public FrameDemo() {
setTitle("Information");//设置窗口标题
setSize(320, 360);//设置窗口大小
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭时退出虚拟机
getContentPane().setLayout(new FlowLayout());//设置窗口布局为流式布局
JPanel jp = new JPanel(new GridLayout(4, 2));//设置jp面板为表格布局4行2列
//第一行
JPanel jp01 = new JPanel();
JLabel jl1 = new JLabel("Name:");
jp01.add(jl1);
JPanel jp1 = new JPanel();
jtf1 = new JTextField(8);
jp1.add(jtf1);
//第二行
JPanel jp02 = new JPanel();
JLabel jl2 = new JLabel("Number:");
jp02.add(jl2);
JPanel jp2 = new JPanel();
jtf2 = new JTextField(8);
jp2.add(jtf2);
//第三行
JPanel jp03 = new JPanel();
JLabel jl3 = new JLabel("Class:");
jp03.add(jl3);
JPanel jp3 = new JPanel();
jtf3 = new JTextField(8);
jp3.add(jtf3);
//第四行
JPanel jp04 = new JPanel();
JLabel jl4 = new JLabel("");
jp04.add(jl4);
JPanel jp4 = new JPanel();
JButton jb = new JButton("确认");
jp4.add(jb);
jp.add(jp01);
jp.add(jp1);
jp.add(jp02);
jp.add(jp2);
jp.add(jp03);
jp.add(jp3);
jp.add(jp04);
jp.add(jp4);
getContentPane().add(jp);
jta = new JTextArea();
jta.setColumns(20);//设置文本域的大小
jta.setEditable(false);//设置文本域不可编辑
jta.setBackground(jp.getBackground());//设置文本域的背景色和面板一样
getContentPane().add(jta);
jb.addActionListener(new ActionListener() {//给按钮添加事件
public void actionPerformed(ActionEvent e) {//点击按钮,显示信息到文本域
String name = jtf1.getText();
String number = jtf2.getText();
String clazz = jtf3.getText();
jta.setText("You name is "+name+" number is "+number+" class is "+clazz);
}
});
}
public static void main(String[] args) {
new FrameDemo().setVisible(true);//创建窗口,被设置为可见
}
}
五: 拓展
虽然图形界面的实现方法是多样的, 我们一定要根据具体情况, 选择一个比较优化的 合理的, 符合业务逻辑的实现方法