程序多窗口功能研究,代码来源于一位大佬的Java教材。
??Gives a program that creates a main window with a text area in the scroll pane
and a button named Show Histogram. When the user clicks the button, a new window appears
that displays a histogram to show the occurrences of the letters in the text area. Figure 17.15
contains a sample run of the program.
??编写一个程序,创建一个主窗口,页面包含文本区域嵌套在滚动面板中,一个按钮名字为显示直方图,当用户点击按钮,一个新的窗口出现,显示的是一个直方图,统计文本区域中每个字符的出现次数,下图显示的是程序运行效果。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultipleWindowDemo extends JFrame {
private JTextArea jta = new JTextArea();
private JButton jbtShowHistogram = new JButton("Show Histogram");
Histogram histogram = new Histogram();
private String content = "Write a program that creates a main window with " +
"a text area in the scroll pane and a button named \"Show Histogram\"." +
"When the user clicks the button, a new window appears that displays "+
"a histogram to show the occurrence of the letters in the text area." +
"Figure 13.31 contains a sample run of the program.";
// private String content = "编写一个程序,创建一个主窗口,页面包含文本区域嵌套在滚动面板中,一个按钮名字为显示直方图" +
// "当用户点击按钮,一个新的窗口出现,显示的是一个直方图,统计文本区域中每个字符的出现次数,图13.31显示的是程序运行效果";
// Create a new frame to hold the histogram panel
private JFrame histogramFrame = new JFrame();
public MultipleWindowDemo() throws HeadlessException {
// Store text area in a scroll pane
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
scrollPane.setPreferredSize(new Dimension(300, 200));
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
add(scrollPane, BorderLayout.CENTER);
add(jbtShowHistogram, BorderLayout.SOUTH);
jta.setText(content);
jbtShowHistogram.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[] count = countLetters();
// Set the letter count to histogram for display
histogram.setHistogram(count);
// Show the frame
histogramFrame.setVisible(true);
}
});
histogramFrame.add(histogram);
histogramFrame.pack();
histogramFrame.setTitle("Histogram");
}
/** Count the letters in the text area */
public int[] countLetters() {
int[] count = new int[26];
String text = jta.getText();
for (int i = 0; i < text.length(); i++) {
char character = text.charAt(i);
if (character >= 'a' && character <= 'z')
count[character - 'a']++;
else if (character >= 'A' && character <= 'Z')
count[character - 'A']++;
}
return count;// Return the count array
}
public static void main(String[] args) {
MultipleWindowDemo frame = new MultipleWindowDemo();
frame.setTitle("MultipleWindowDemo");
//frame.pack();
frame.setSize(300, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
package learn2;
import javax.swing.*;
import java.awt.*;
public class Histogram extends JPanel {
// Count the occurrences of 26 letters
int[] count;
public Histogram() {
}
public void setHistogram(int[] count) {
this.count = count;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
if (count == null) // no display if count is null
return;
super.paintComponent(g);
// Find the panel size and bar width and interval dynamically
int width = getWidth();
int height = getHeight();
int interval = (width - 40) / count.length;
int individualWidth = (int) ((width - 40) / 24 * 0.60);
// Find the maximum count. The maximum count has the highest bar
int maxCount=0;
for (int i = 0; i < count.length; i++) {
if (count[i] > maxCount){
maxCount = count[i];
}
}
// X is the start position for the first bar in the histogram
int x = 20;
// Draw a horizontal base line
g.drawLine(10, height - 45, width - 10, height - 45);
for (int i = 0; i < count.length; i++) {
// find the bar height
int barHeight = (int) (((double)count[i] / (double) maxCount) * (height - 55));
// Display a bar (i.e. rectangle)
g.drawRect(x, height - 45 - barHeight, individualWidth, barHeight);
// Display a letter under the base line
g.drawString(String.valueOf((char)(65 + i)), x, height - 30);
x += interval;
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
??在程序开发中有时候需要实现多个窗口,比如打开一个窗口实现特定的任务,在游戏开发中,可以实现一个主窗口,显示一些基本操作,游戏简介等,游戏窗口就可以创建一个新窗口,负责游戏的渲染和交互。
??当然不止游戏,许多程序都有多窗口的需求,可以根据需要来实现。
??Java Swing的JPanel相当于Android中的View类,paintComponent(Graphics g)
方法相当于onDraw(Canvas c)
方法,一般用于实现一些自定义的绘制需求。JPanel还可以充当子面板,用于实现一些较复杂的页面布局。