9.4.3 边框 边框:分隔出多个组别 可选效果: 凸出 凹陷 刻线 线条 粗线条 添加标题: BoderFactory.createTitledBorder 组合边框: BorderFacotory.createCompoundBorder 把边框添加到组件: Border etched
9.4.3 边框
边框:分隔出多个组别
可选效果:
- 凸出
- 凹陷
- 刻线
- 线条
- 粗线条
添加标题:
BoderFactory.createTitledBorder
组合边框:
BorderFacotory.createCompoundBorder
把边框添加到组件:
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createEtchedBorder(etched, "A title");
Panel.setBorder(titled);
import javax.swing.*;import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
BorderFrame frame = new BorderFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BorderFrame extends JFrame{
private JPanel demoPanel;
private JPanel buttonPanel;
private ButtonGroup group;
private static final int SIZE = 12;
public BorderFrame(){
setTitle("BorderTest");
setSize(600,200);
demoPanel = new JPanel();
buttonPanel = new JPanel();
group = new ButtonGroup();
addRadioButton("Lower Level",BorderFactory.createLoweredBevelBorder());
addRadioButton("Raised Bevel",BorderFactory.createRaisedBevelBorder());
addRadioButton("Etched",BorderFactory.createEtchedBorder());
addRadioButton("Line",BorderFactory.createLineBorder(Color.BLUE));
addRadioButton("Matte",BorderFactory.createMatteBorder(10,10,10,10,Color.BLUE));
addRadioButton("Empty",BorderFactory.createEtchedBorder());
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched,"Border types");
buttonPanel.setBorder(titled);
setLayout(new GridLayout(2,1));
add(buttonPanel);
add(demoPanel);
}
private void addRadioButton(String buttonName, final Border b){
JRadioButton button = new JRadioButton(buttonName);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
demoPanel.setBorder(b);
}
});
group.add(button);
buttonPanel.add(button);
}
}
9.4.4 组合框
有时候我们希望既能编辑,又有下拉菜单的默认选项可以选择。这样有多个功能的编辑框,就佳作组合框。
import javax.swing.*;import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxFrame frame = new ComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ComboBoxFrame extends JFrame{
private JComboBox faceCombo;
private JLabel label;
private static final int SIZE = 12;
public ComboBoxFrame(){
setTitle("ComboBoxTest");
setSize(600,200);
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif",Font.PLAIN,SIZE));
add(label,BorderLayout.CENTER);
faceCombo = new JComboBox();
faceCombo.setEditable(true);
faceCombo.addItem("Serif");
faceCombo.addItem("SansSerif");
faceCombo.addItem("Monospaced");
faceCombo.addItem("Dialog");
faceCombo.addItem("DialogInput");
faceCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setFont(new Font((String)faceCombo.getSelectedItem(),Font.PLAIN,SIZE));
}
});
JPanel comboPanel = new JPanel();
comboPanel.add(faceCombo);
add(comboPanel, BorderLayout.SOUTH);
}
}
效果是:选中或输入已有元素,则自动设置成对应字体;输入不存在的字体时,默认选择第一个。
相关内容:选择 《Java核心技术 卷1》查找相关笔记
喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。
关注公众号,互相学习:钰娘娘知识汇总