?? musicentrydialog.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
/**
* 增加新的CD信息
*/
public class MusicEntryDialog extends JDialog {
protected Frame parentFrame;
protected ArrayList categoryArrayList;
protected JTextField artistTextField;
protected JTextField titleTextField;
protected JComboBox categoryComboBox;
protected boolean okButtonPressed = false;
protected JButton okButton;
protected JButton cancelButton;
protected DefaultListModel trackListModel;
public MusicEntryDialog(Frame theParentFrame, ArrayList theCategories) {
this(theParentFrame, "Add A New Recording", theCategories);
}
public MusicEntryDialog(Frame theParentFrame, String theTitle, ArrayList theCategories) {
super(theParentFrame, theTitle, true); // creates a modal dialog
parentFrame = theParentFrame;
categoryArrayList = theCategories;
buildGui();
}
private void buildGui() {
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
JPanel infoPanel = new JPanel();
infoPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
infoPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
c.weightx = 0.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5, 0, 2, 5);
JLabel artistLabel = new JLabel("Artist: ");
artistLabel.setForeground(Color.black);
infoPanel.add(artistLabel, c);
c.gridy = 2;
c.insets = new Insets(2, 0, 2, 5);
JLabel titleLabel = new JLabel("Title: ");
titleLabel.setForeground(Color.black);
infoPanel.add(titleLabel, c);
c.gridy = 3;
c.insets = new Insets(2, 0, 10, 5);
JLabel categoryLabel = new JLabel("Category: ");
categoryLabel.setForeground(Color.black);
infoPanel.add(categoryLabel, c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(2, 0, 0, 5);
artistTextField = new JTextField(15);
infoPanel.add(artistTextField, c);
c.gridy = 2;
c.insets = new Insets(2, 0, 10, 5);
titleTextField = new JTextField(15);
infoPanel.add(titleTextField, c);
categoryComboBox = new JComboBox();
Iterator iterator = categoryArrayList.iterator();
String aCategory;
while (iterator.hasNext()) {
aCategory = (String) iterator.next();
categoryComboBox.addItem(aCategory);
}
c.gridy = 3;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.WEST;
infoPanel.add(categoryComboBox, c);
c.gridy = 4;
JButton addTrackButton = new JButton("增加 Track...");
addTrackButton.setToolTipText("點擊以便增加信息");
infoPanel.add(addTrackButton, c);
container.add(BorderLayout.NORTH, infoPanel);
trackListModel = new DefaultListModel();
JList tracksListBox= new JList(trackListModel);
JScrollPane tracksScrollPane = new JScrollPane(tracksListBox);
TitledBorder listBorder = BorderFactory.createTitledBorder("List of Tracks");
listBorder.setTitleColor(Color.black);
tracksScrollPane.setBorder(listBorder);
container.add(BorderLayout.CENTER, tracksScrollPane);
JPanel bottomPanel = new JPanel();
okButton = new JButton("OK");
bottomPanel.add(okButton);
cancelButton = new JButton("Cancel");
bottomPanel.add(cancelButton);
container.add(BorderLayout.SOUTH, bottomPanel);
addTrackButton.addActionListener(new TrackActionListener());
ActionListener buttonListener = new OkCancelActionListener();
okButton.addActionListener(buttonListener);
cancelButton.addActionListener(buttonListener);
this.pack();
Point parentLocation = parentFrame.getLocation();
this.setLocation(parentLocation.x + 50, parentLocation.y + 50);
}
public boolean isOkButtonPressed() {
return okButtonPressed;
}
public MusicRecording getMusicRecording() {
String artist = artistTextField.getText();
String title = titleTextField.getText();
String category = (String) categoryComboBox.getSelectedItem();
int basePrice = 9 + (int) (Math.random() * 7);
double price = basePrice + .99;
int size = trackListModel.getSize();
Track[] trackList = new Track[size];
for (int i=0; i < size; i++) {
trackList[i] = (Track) trackListModel.getElementAt(i);
}
MusicRecording theRecording = new MusicRecording(artist, trackList, title,
price, category, "blank.gif");
return theRecording;
}
class OkCancelActionListener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == okButton) {
okButtonPressed = true;
}
else {
okButtonPressed = false;
}
setVisible(false);
}
}
class TrackActionListener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
TrackEntryDialog myTrackDialog = new TrackEntryDialog(parentFrame);
myTrackDialog.setVisible(true);
if (myTrackDialog.isOkButtonPressed()) {
Track theTrack = myTrackDialog.getTrack();
trackListModel.addElement(theTrack);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -