代码如下锋悉帆:
import java.awt.Colorimport java.awt.Dimension
import java.awt.FlowLayout
import java.util.HashMap
import java.util.Map
import javax.swing.JComboBox
import javax.swing.JFrame
import javax.swing.JTextField
public class App extends JFrame {
private JComboBox<String> cbxColors
private JTextField txtName
private Map<String, Color> colorMap
public App() {
this.setSize(300, 100)
this.setLocationRelativeTo(null)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setLayout(new FlowLayout())
colorMap = new HashMap<>()
colorMap.put("红"陆行, Color.RED)
colorMap.put("绿", Color.GREEN)
colorMap.put("蓝", Color.BLUE)
colorMap.put("黄", Color.YELLOW)
cbxColors = new JComboBox(colorMap.keySet().toArray())
cbxColors.setPreferredSize(new Dimension(50, 23))
cbxColors.addItemListener(e 银雹-> {
Color color = colorMap.get(e.getItem())
txtName.setBackground(color)
})
this.add(cbxColors)
txtName = new JTextField()
txtName.setPreferredSize(new Dimension(100, 23))
txtName.setBackground(Color.RED)
this.add(txtName)
}
public static void main(String[] args) {
new App().setVisible(true)
}
}
运行结果:
首先:采用什么技术实现
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 JComboBox<String> 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 JComboBox<String>()
t1.setModel(new DefaultComboBoxModel<String>(new String[] { "林老师", "赵老师", "孙老师" }))
panel_2.add(t1)
JPanel panel_3 = new JPanel()
panel.add(panel_3)
cb2 = new JCheckBox("世界经济")
panel_3.add(cb2)
t2 = new JComboBox<String>()
t2.setModel(new DefaultComboBoxModel<String>(new String[] { "张老师", "刘老师" }))
panel_3.add(t2)
JPanel panel_4 = new JPanel()
panel.add(panel_4)
cb3 = new JCheckBox("音乐赏析")
panel_4.add(cb3)
t3 = new JComboBox<String>()
t3.setModel(new DefaultComboBoxModel<String>(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("")
}
})
}
}
就这么着吧,有点长。/*
* 输入一个拆乎旦数,输出所有素数。
* 2008-10-16
*/
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.util.ArrayList
import java.util.List
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.JTextField
public class OutputPrime {
private JTextArea textArea
private JTextField textField
private JFrame frame
private List<Integer>list = new ArrayList<Integer>()
public static void main(String args[]) {
OutputPrime window = new OutputPrime()
window.frame.setVisible(true)
}
public OutputPrime() {
createContents()
}
private void createContents() {
frame = new JFrame()
frame.setResizable(false)
frame.getContentPane().setLayout(null)
frame.setBounds(100, 100, 274, 309)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
final JLabel label = new JLabel()
label.setText("顷渣输入框:")
label.setBounds(10, 10, 52, 37)
frame.getContentPane().add(label)
textField = new JTextField()
textField.setBounds(68, 17, 185, 22)
frame.getContentPane().add(textField)
final JScrollPane scrollPane = new JScrollPane()
scrollPane.setBounds(10, 45, 243, 153)
frame.getContentPane().add(scrollPane)
textArea = new JTextArea()
scrollPane.setViewportView(textArea)
textArea.setEditable(false)
textArea.setLineWrap(true)
final JButton button = new JButton()
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
getResult()
button.setEnabled(false)
textField.setEditable(false)
}
})
button.setText("旅扰结果")
button.setBounds(10, 204, 243, 28)
frame.getContentPane().add(button)
final JButton button_1 = new JButton()
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
textArea.setText("")
textField.setText("")
button.setEnabled(true)
textField.setEditable(true)
list.clear()
}
})
button_1.setText("重置")
button_1.setBounds(10, 238, 243, 28)
frame.getContentPane().add(button_1)
}
private void getResult() {
int input
if(textField.getText().equals("") || textField.getText()==null){
textArea.setText("请输入一个大于等于2的正整数")
}else{
input = Integer.parseInt(textField.getText())
if (input<2) {
textArea.setText("请输入一个大于等于2的正整数")
} else {
String str
List tempList = isPrime(input)
int temp = tempList.size()
for(int i = 0i<tempi++){
str = (String) tempList.get(i).toString()
textArea.append(str + ",")
}
textArea.append("素数个数为" + temp + "个")
}
}
}
private List<Integer>isPrime(int input) {
outer:
for (int i = 2i <= inputi++) {
for (int j = 2j <= Math.sqrt(i)j++) {
if(i % j == 0){
continue outer
}
}
list.add(i)
}
return list
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)