📄 lineformatdialog.java
字号:
Integer[] styleIntArray = new Integer[styleStrings.length];
styleImages = new ImageIcon[styleStrings.length];
for (int i = 0; i < styleStrings.length; i++) {
styleIntArray[i] = new Integer(i);
styleImages[i] = ToolFactory.createIcon("line/"+styleStrings[i] + "32");
if (styleImages[i] != null) {
styleImages[i].setDescription(styleStrings[i]);
}
}
//Create the style combo box.
ComboBoxRenderer renderer1= new ComboBoxRenderer(styleImages,styleStrings);
renderer1.setPreferredSize(new Dimension(60, 32));
lineStyleList = new JComboBox(styleIntArray);
lineStyleList.setRenderer(renderer1);
lineStyleList.setSelectedIndex(lineFormat.getLineStyle());
lineStyleList.setMaximumRowCount(5);
//Load the widthImages and create an array of indexes.
Integer[] widthIntArray = new Integer[widthStrings.length];
widthImages = new ImageIcon[widthStrings.length];
for (int i = 0; i < widthStrings.length; i++) {
widthIntArray[i] = new Integer(i);
widthImages[i] = ToolFactory.createIcon("line/"+widthStrings[i] + "32");
if (widthImages[i] != null) {
widthImages[i].setDescription(widthStrings[i]);
}
}
//Create the width combo box.
ComboBoxRenderer renderer2= new ComboBoxRenderer(widthImages,widthStrings);
renderer2.setPreferredSize(new Dimension(60, 32));
lineWidthList = new JComboBox(widthIntArray);
lineWidthList.setRenderer(renderer2);
lineWidthList.setSelectedIndex(lineFormat.getLineWidth());
lineWidthList.setMaximumRowCount(5);
//Create a color picker button.
colorPickerButton = new JButton("");
colorPickerButton.setMargin(new java.awt.Insets(0,0,0,0));
colorPickerButton.setPreferredSize(new Dimension(30,30));
colorPickerButton.setActionCommand("ChangeColor");
colorPickerButton.addActionListener(this);
GUIUtils.setButtonBackground(colorPickerButton,lineFormat.getLineColor());
m_lineColor =new Color(lineFormat.getLineColor().getRGB());
//add color picker button to a small panel.
JPanel colorPickerPanel =new JPanel();
colorPickerPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
colorPickerPanel.add(colorPickerButton,BorderLayout.CENTER);
//add combobox and color picker button to container.
JPanel comboPanel =new JPanel();
comboPanel.setLayout(new BoxLayout(comboPanel,BoxLayout.Y_AXIS));
comboPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
comboPanel.add(Box.createRigidArea(new Dimension(0,10)));
comboPanel.add(lineStyleList);
comboPanel.add(Box.createRigidArea(new Dimension(0,10)));
comboPanel.add(lineWidthList);
comboPanel.add(Box.createRigidArea(new Dimension(0,10)));
comboPanel.add(colorPickerPanel);
//add label container and combobox container to a new container(chooser).
JPanel chooserPane = new JPanel();
chooserPane.setLayout(new BoxLayout(chooserPane, BoxLayout.X_AXIS));
chooserPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
chooserPane.add(Box.createHorizontalGlue());
chooserPane.add(labelPanel);
chooserPane.add(Box.createRigidArea(new Dimension(10, 0)));
chooserPane.add(comboPanel);
//Lay out the buttons from left to right.
JPanel buttonPane = new JPanel();
//buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(Box.createRigidArea(new Dimension(15, 0)));
buttonPane.add(defaultButton);
buttonPane.add(Box.createRigidArea(new Dimension(15, 0)));
buttonPane.add(confirmButton);
buttonPane.add(Box.createRigidArea(new Dimension(15, 0)));
buttonPane.add(cancelButton);
//Put everything together, using the content pane's BorderLayout.
Container contentPane = getContentPane();
contentPane.add(chooserPane, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(locationComp);
}
//Handle clicks on the Set and Cancel buttons.
public void actionPerformed(ActionEvent e) {
String cmd =e.getActionCommand();
if ("ChangeColor".equals(cmd)){
Color newColor = JColorChooser.showDialog(
LineFormatDialog.this,
CADResource.getString("dialog.pickColor"),
m_lineColor);
if (newColor != null) {
GUIUtils.setButtonBackground(colorPickerButton,newColor);
m_lineColor =newColor;
}
return;
}else if ("Confirm".equals(cmd) || "Default".equals(cmd)) {
m_lineFormat.setLineStyle(lineStyleList.getSelectedIndex());
m_lineFormat.setLineWidth(lineWidthList.getSelectedIndex());
m_lineFormat.setLineColor(m_lineColor);
if ("Default".equals(cmd)){
GlobalSettings settings =GlobalSettings.getInstance();
settings.setLineFormat(m_lineFormat);
JOptionPane.showMessageDialog(null, CADResource.getString("message.defaultvalue.lineformat"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE);
return;
}
m_dialog.setVisible(false);
}else{
m_lineFormat =null;
m_dialog.setVisible(false);
}
}
class ComboBoxRenderer extends JLabel
implements ListCellRenderer {
private ImageIcon[] m_itemImages;
private String[] m_itemText;
public ComboBoxRenderer(ImageIcon[] itemImages, String[] itemText) {
m_itemImages =itemImages;
m_itemText =itemText;
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
//Get the selected index. (The index param isn't
//always valid, so just use the value.)
int selectedIndex = ((Integer)value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
//Set the icon and text. If icon was null, say so.
ImageIcon icon;
String itemText;
icon = m_itemImages[selectedIndex];
itemText = m_itemText[selectedIndex];
setIcon(icon);
if (icon == null) {
setText(itemText);
setFont(list.getFont());
}
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -