在本练习中,当您输入城市的名称及其人口时,必须在窗口中绘制一个代表该城市人口的条形图(如图所示),该练习几乎完成了,但是没有用,有人能告诉我我在做什么错吗?,为什么没有画出条形图?使我对练习更加困难的条件是,如果窗口尺寸改变,则条形图和线条的尺寸必须改变。因为已经给出了主类,所以不能对其进行修改。
package Hw02_Statistics;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import hw01_clocknumbers.DigitalNumber;
public class Statistics extends JFrame {
private JLabel city, population, other;
private JTextField tcity, tpopulation;
private JButton add;
private JTable table;
private DefaultTableModel tablem;
private int index=1;
private bar bars;
public Statistics() {
setTitle("Statistics");
setSize(500, 350);
setupWidgets();
seupEvents();
setVisible(true);
}
private void setupWidgets() {
bars = new bar();
city = new JLabel("City",JLabel.LEFT);
population = new JLabel("Population",JLabel.LEFT);
tcity = new JTextField();
other = new JLabel();
tpopulation = new JTextField();
add = new JButton("Add");
tablem = new DefaultTableModel(new Object[] {"Index", "City", "Population"}, 0);
table = new JTable(tablem);
JPanel norte = new JPanel(new GridLayout(5,1));
JPanel sur = new JPanel(new GridLayout(1,2));
add(norte, BorderLayout.NORTH);
add(sur, BorderLayout.CENTER);
norte.add(city);
norte.add(tcity);
norte.add(population);
norte.add(tpopulation);
norte.add(add);
sur.add(new JScrollPane(table));
sur.add(bars);
}
private void seupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!tcity.getText().equals("") && !tpopulation.getText().equals("")) {
Object rowinfo[] = {index, tcity.getText(), tpopulation.getText()};
tablem.addRow(rowinfo);
int n= Integer.parseInt(tpopulation.getText());
bars.setScale(1, index);
tcity.setText("");
tpopulation.setText("");
index++;
}
}
});
}
public static void main(String[] args) {
new Statistics();
}
}
这是我使用的第二堂课
package Hw02_Statistics;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JComponent;
import hw01_clocknumbers.DigitalNumber;
public class bar extends JComponent {
private int digit;
private Integer scale=0;
private int index;
private rect rects[];
public bar () {
}
public void paint(Graphics g) {
int w =getWidth();
int h =getHeight();
rects = new rect[4];
rects[index] = new rect(scale, index, w, h);
// System.out.println(w); 243
// System.out.println(h); 183
g.drawLine(w/12, h/9, w/12, 8*h/9);
g.drawLine(w/12, 8*h/9, 12*w/12, 8*h/9);
}
public void setScale(Integer scale, int index) {
this.index=index-1;
this.scale=scale;
repaint();
}
}
这是我使用的最后一个类,它是无效的
package Hw02_Statistics;
import java.awt.Graphics;
import javax.swing.JComponent;
public class rect extends JComponent{
private int scale;
private int index;
private int w, h;
public rect(int scale, int index, int w, int h) {
this.scale = scale;
this.index= index;
this.w =w;
this.h=h;
}
public void paint(Graphics e) {
e.fillRect(6*w/12+w*index/12,h/9,scale*w/12,scale*7*h/9);
System.out.println("Aaaa");
repaint();
}
}
最佳答案
类bar
(根据Java命名约定应称为Bar
)已重命名为Bars
,并更改为保留和绘制所有条形:
class Bars extends JComponent {
//collection of bar heights
private final List<Integer> barsValues;
private static final int W = 20; //bar width
public Bars () {
barsValues = new ArrayList<>();
}
//override paintComponent rather than paint
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); //always call super
int w = getWidth();
int h = getHeight();
//use double to avoid rounding errors
double minX = w/12 , maxX = 12*minX, maxY = h/9, minY = 8 * maxY;
double graphHeight = minY - maxY;
//draw axis
g.drawLine((int)minX, (int)maxY, (int)minX, (int)minY);
g.drawLine((int)minX, (int)minY, (int)maxX, (int)minY);
//draw bars
if(barsValues.size() == 0) return;
double x = minX + W ;
int maxHeight = getMaxHeight(); //calculate scale based on max height
double scale = maxHeight > 0 ? graphHeight / getMaxHeight() : 1;
for(int barValue : barsValues){
double barHeight = scale*barValue;
g.fillRect((int)x ,(int)(minY - barHeight), W, (int)barHeight);
x += 2*W;
}
}
//add bar values. valid values should be > 0
void addBar(int height) {
barsValues.add(height);
repaint();
}
//get max height
int getMaxHeight(){
int max = 0;
for (int value : barsValues){
if(value > max) {
max = value;
}
}
return max;
}
}
不需要类
rect
。要进行测试,您需要在
Statistics
中做一些小的更改:将
private bar bars;
更改为private Bars bars
;通过
bars = new Bars();
初始化并将动作侦听器中的一行从
bars.setScale(1, index)
更改为bars.addBar(n);
完整的代码可以从here复制粘贴
关于java - 在此示例中如何绘制条形图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54849017/