Java,
Java,
文本编辑组件JEditorPane和JtextPane
JeditorPane editorPane=new JeditorPane();
这时会创建一个空白的JeditorPane对象,默认情况下时刻编辑的,如果只需要显示信息,而不需要编辑,可使用方法setEditable(false)将其设为不可编辑的
EditorPane.setEditable(false);
要获得用户在编辑时输入的文字,可用如下
String inpu =teditor.getText();
添加到一个滚动面板中
Javax.swing. JscrollPane
JscrollPane editorJScrollPane=new JscrollPane(editorPane);
JtextArea textArea=new JtextArea(3,20);
JscrollPane editorJScrollPane=new JscrollPane(textArea);;
textArea.setEditable(false);
工具栏JtoolBar
JtoolBar toolBar;=new JtoolBar();
Jbutton button=new Jbutton(:J’
toolBar.add(button);
使用面板做子容器
假设要在框架中放置十个按钮和一个文本域,按钮以网格形式放置文本域单独占一行
用swing版本的Jpanel
Jpanel p=new Jpanel();
p.add(new Jbutton(“ok”));
面板可以放到框架或另一个年版中,下列语句将面板p放置到框架f中
f..getcontentPane().add(p);
一个按钮
|
一个文本域
12个按钮
Import java.awt.*;
Import java.swing.*;
Publiic class TestPanels extends Jframe
{
Public TestPanels()
{
Container container=getContentPane();
Container.setLayout(new BorderLayout());
Jpanel p1=new Jpanel();
P1.setLayout(new GridLayout(4,3));
for(int i=1;i<9,i++)
{
P1.add(new Jbutton(“ “+i);
}
P1.add(new Jbutton(“ “+0));
P1.add(new Jbutton(“start”));
P1.add(new Jbuttton(“stop”));
Jpanel p2=new Jpanel(new BorderLayout());
P2.add(new JtextField(“Time to be displayedhere”),BorderLayout.NORTH);
P2.add(p1,BorderLayout.center);
Container.add(p2,BorderLayout.EAST);
Container.add(new Jbutton(“food to beplaced here”),BorderLayout.CENTER);
}
Public static void main()
{
TestPanels Frame=new TestPanels();
Frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE));
Frame.setSize(400,250);
Frame.setVisible(true);
}
}
单选按钮,复选按钮,复选框和下拉列表
可以用JradioButton类开发单选按钮
Public JradioButton(String text,booleanselected)
其中参数1为单选按钮标题,参数2为选择状态
既然单选按钮支持的是多选一,怎样将多个按钮看成一组呢?可以用javax.swing.ButtonGroup实现,该类有个add函数,能将多个按钮加入看成一组。但是ButtonGroup不能被加到界面上,还要将按钮一个一个加到界面上去
Private JradioButton s1=new RadioButton(“ “)
.
.
ButtonGroup ss=new ButtonGroup();
ss.add(s1);
下拉列表框
可以用JcomboBox开发一个下拉列表框
Javax.swing。JcomboBox类
实例化一个下拉列表框可以用addItem函数添加
复选框
复选框是提供多选功能(可以不选,可以多选,也可以选一部分)
可以用JcheckBox类开发复选框找到javax.swing.JcheckBox类
用以下代码显示上面几种控件
importjavax.swing.*;
publicclass ASS10 {
publicstaticvoid main(String[] args) {
double balance=0;
while(true)
{
String str=
JOptionPane.showInputDialog("0:退出, 1:存款, 2:取款,3:查询余额");
int ch=Integer.parseInt(str);
if(ch==0)
{
JOptionPane.showMessageDialog(null,"谢谢光临" );
break;
}
elseif(ch==1)
{
str=JOptionPane.showInputDialog("输入钱数");
double money=Double.parseDouble(str);
balance+=money;
JOptionPane.showMessageDialog(null,"存款成功");
}
elseif(ch==2)
{
str=JOptionPane.showInputDialog("输入钱数");
double money=Double.parseDouble(str);
if(balance>money)
{
balance-=money;
JOptionPane.showMessageDialog(null,"取款成功");
}
else
{
JOptionPane.showMessageDialog(null,"取款失败");
}
}
elseif(ch==3)
{
JOptionPane.showMessageDialog(null,"余额是"+balance);
}
}
}
}
import javax.swing.*;
public class ASS10
{
public static void main(String[] args) {
double balance=0;
while(true)
{
String str=
JOptionPane.showInputDialog("0:退出, 1:存款, 2:取款,3:查询余额");
int ch=Integer.parseInt(str);
if(ch==0)
{
JOptionPane.showMessageDialog(null,"谢谢光临" );
break;
}
else if(ch==1)
{
str=JOptionPane.showInputDialog("输入钱数");
double money=Double.parseDouble(str);
balance+=money;
JOptionPane.showMessageDialog(null,"存款成功");
}
else if(ch==2)
{
str=JOptionPane.showInputDialog("输入钱数");
double money=Double.parseDouble(str);
if(balance>money)
{
balance-=money;
JOptionPane.showMessageDialog(null,"取款成功");
}
else
{
JOptionPane.showMessageDialog(null, "取款失败");
}
}
else if(ch==3)
{
JOptionPane.showMessageDialog(null, "余额是"+balance);
}
}
}
}
package event;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class EvenTest4 extends JFrame implements ActionListener{
private JButton btLogin=new JButton("确定");
private JButton btExit=new JButton("退出");
public EvenTest4()
{
this.setLayout(new FlowLayout());
this.add(btLogin);
this.add(btExit);
btLogin.addActionListener(this);
btExit.addActionListener(this);
this.setVisible(true);
this.setSize(100,100);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btLogin)//此处使用的e.getSource()判断事件是有谁发生的
{
System.out.println("登陆");
}
else
{
System.exit(0);
}
}
public static void main(String[] args) {
new EvenTest4();
}
}
相关文章
- 暂无相关文章
用户点评