扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.Checkbox;
创新互联建站专注于石门企业网站建设,成都响应式网站建设公司,成都做商城网站。石门网站建设公司,为石门等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
public class Choose extends JFrame implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label l1, l2;
TextField t1, t2;
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("QQ", checkboxGroup, false);
Checkbox checkbox2 = new Checkbox("MSN", checkboxGroup, false);
Checkbox checkbox3 = new Checkbox("ICQ", checkboxGroup, false);
Choice c;
public Choose() {
super("简单小程序");
this.setLayout(new FlowLayout());
l1 = new Label("选择你常用的软件:");
l2 = new Label("选择你喜欢的水果:");
checkbox1.addItemListener(this);
checkbox2.addItemListener(this);
checkbox3.addItemListener(this);
t1 = new TextField(20);
t2 = new TextField(20);
c = new Choice();
c.addItemListener(this);
c.add("苹果");
c.add("橘子");
c.add("香蕉");
c.add("梨子");
this.add(l1);
this.add(checkbox1);
this.add(checkbox2);
this.add(checkbox3);
this.add(t1);
this.add(l2);
this.add(c);
this.add(t2);
this.setSize(450, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new Choose();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == checkbox1) {
t1.setText("你常用的软件是:" + checkbox1.getLabel());
}
if (e.getSource() == checkbox2) {
t1.setText("你常用的软件是:" + checkbox2.getLabel());
}
if (e.getSource() == checkbox3) {
t1.setText("你常用的软件是:" + checkbox3.getLabel());
}
t2.setText("你喜欢的水果是:" + c.getSelectedItem());// 得到选中的下拉列表值
}
}
Shape.java接口代码
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
利用JAVA类库中的CheckboxGroup,具体可以参考API,
然后将性别选项(Checkbox)添加到CheckboxGroup中去。
这样就在任何时刻都有且仅有一个被选中了。
你可以去试下。很简单的。
Random ran = new Random();
int result = ran.nextInt(4) + 1;
//result就是1-4的一个随机数
//下面是个完整例子
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
Random ran = new Random();
int result = ran.nextInt(4) + 1;
switch(result) {
case 1:{
System.out.println("A");
}
case 2:{
System.out.println("B");
}
case 3:{
System.out.println("C");
}
case 4:{
System.out.println("D");
}
}
}
你可以把switch语句删掉,改成你自己的代码。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
public class Tabbepan_test extends JFrame{
private static final long serialVersionUID = 7382825389354474881L;
JPanel contpane,p1,p2,p3,p4,p5,p6,p7;
JScrollPane jsc,jsc1;
JButton btn;
JTabbedPane tabp;
JTree tree;
JSplitPane jsp;
DefaultMutableTreeNode node,top;
DefaultTableModel tableModel;
public Tabbepan_test(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contpane = new JPanel(new BorderLayout());
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
p7 = new JPanel();
node = new DefaultMutableTreeNode("功能导航");
node.add(new DefaultMutableTreeNode("人事管理"));
node.add(new DefaultMutableTreeNode("生产管理"));
node.add(new DefaultMutableTreeNode("资材管理"));
node.add(new DefaultMutableTreeNode("营销管理"));
node.add(new DefaultMutableTreeNode("财务管理"));
node.add(new DefaultMutableTreeNode("总务管理"));
node.add(new DefaultMutableTreeNode("系统维护"));
top = new DefaultMutableTreeNode("ERP系统");
top.add(node);
tree = new JTree(top);
tabp = new JTabbedPane();
tabp.add("人事管理", new JScrollPane(p1));
tabp.add("生产管理", new JScrollPane(p2));
tabp.add("资材管理", new JScrollPane(p3));
tabp.add("营销管理", new JScrollPane(p4));
tabp.add("财务管理", new JScrollPane(p5));
tabp.add("总务管理", new JScrollPane(p6));
tabp.add("系统维护", new JScrollPane(p7));
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
// TODO 自动生成的方法存根
DefaultMutableTreeNode nodes=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String tn = null;
int b = tabp.getTabCount();
for(int i=0;itabp.getTabCount();i++){
tn = tabp.getTitleAt(i).toString();
if(tn.equals(nodes.toString())){
tabp.addTab(nodes.toString(), p1);
}else {
}
}
}
});
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jsp.setDividerSize(1);
jsp.setDividerLocation(170);
jsp.setOneTouchExpandable(false);//让分割线显示出箭头
jsp.setContinuousLayout(true);//操
jsc1 = new JScrollPane(tree);
jsp.setLeftComponent(jsc1);
jsp.setRightComponent(tabp);
contpane.add(jsp,BorderLayout.CENTER);
setContentPane(contpane);
setSize(800,600);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO 自动生成的方法存根
Tabbepan_test test = new Tabbepan_test();
test.setVisible(true);
}
});
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流