⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trackentrydialog.java

📁 《Java核心技术应用开发》电子工业出版社书籍源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import java.util.*;

/**
 *  这个类用来增加歌曲
 */
public class TrackEntryDialog extends JDialog {

	
	protected Frame parentFrame;
		
	public JTextField trackTitleTextField;
	public JTextField titleTextField;
	public JTextField minutesTextField;
	public JTextField secondsTextField;

	protected boolean okButtonPressed = false;
	protected JButton okButton;  	
	protected JButton cancelButton;
	
	
	public TrackEntryDialog(Frame theParentFrame) {

		this(theParentFrame, "增加新歌");	
	}
	
	
	public TrackEntryDialog(Frame theParentFrame, String theTitle) {
	
		super(theParentFrame, theTitle, true);		

		parentFrame = theParentFrame;

		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.HORIZONTAL;
		c.anchor = GridBagConstraints.WEST;
		c.insets = new Insets(5, 0, 5, 5);
		JLabel trackTitleLabel = new JLabel("Track Title:  ");
		trackTitleLabel.setForeground(Color.black);
		infoPanel.add(trackTitleLabel, c);

		c.gridx = 3;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1.0;
		c.insets = new Insets(2, 0, 5, 5);
		trackTitleTextField = new JTextField(15);
		infoPanel.add(trackTitleTextField, c);
				
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		c.weightx = 0.0;
		c.insets = new Insets(2, 0, 15, 5);
		JLabel titleLabel = new JLabel("Duration -  ");
		titleLabel.setForeground(Color.black);
		infoPanel.add(titleLabel, c);

		c.gridx = 3;
		c.anchor = GridBagConstraints.EAST;
		JLabel minutesLabel = new JLabel("Minutes:  ");
		minutesLabel.setForeground(Color.black);
		infoPanel.add(minutesLabel, c);
		
		c.gridx = GridBagConstraints.RELATIVE;
		c.anchor = GridBagConstraints.WEST;
		minutesTextField = new JTextField(3);
		infoPanel.add(minutesTextField, c);
		
		c.anchor = GridBagConstraints.EAST;
		JLabel secondsLabel = new JLabel("Seconds:  ");
		secondsLabel.setForeground(Color.black);
		infoPanel.add(secondsLabel, c);
		
		c.anchor = GridBagConstraints.WEST;
		secondsTextField = new JTextField(3);
		infoPanel.add(secondsTextField, c);
		
		container.add(BorderLayout.NORTH, infoPanel);
		
		JPanel bottomPanel = new JPanel();
		okButton = new JButton("OK");
		bottomPanel.add(okButton);
		
		cancelButton = new JButton("Cancel");
		bottomPanel.add(cancelButton);
		
		container.add(BorderLayout.SOUTH, bottomPanel);
		
		ActionListener buttonListener = new OkCancelActionListener();
		okButton.addActionListener(buttonListener);
		cancelButton.addActionListener(buttonListener);
				
		this.pack();
		
		Point parentLocation = parentFrame.getLocation();
		this.setLocation(parentLocation.x + 100, parentLocation.y + 100);
	}
	
	public boolean isOkButtonPressed() {
		
		return okButtonPressed;
	}
	
	public Track getTrack() {
	
		String title = trackTitleTextField.getText();
		String minutesString = minutesTextField.getText();
		String secondsString = secondsTextField.getText();
		
		int hours = 0;
		int minutes = 0;
		int seconds = 0;
		
		try {
			minutes = Integer.parseInt(minutesString);
		}
		catch (Exception e) {
		  e.printStackTrace();
		}

		try {
			seconds = Integer.parseInt(secondsString);
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		Duration trackDuration = new Duration(hours, minutes, seconds);
		Track theTrack = new Track(title, trackDuration);
		
		return theTrack;
	}
	
	
	class OkCancelActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent event)
		{
			Object source = event.getSource();
			
			if (source == okButton) {
				okButtonPressed = true;
			}
			else {
				okButtonPressed = false;
			}
			
			setVisible(false);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -