我正在构建一个程序,显示给定位置的温度(参见屏幕截图)我想在displayPanal(JPanal)中显示结果。我将paint组件放在displayPanel的扩展类中。但它没有显示任何东西。我只是尝试使用g.drawString方法显示“Hello”来测试,但它没有显示任何内容。所以我被迫继续前进。
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}//main
private static void createAndShowGUI(){
GridLayout panelLayout = new GridLayout(3,0);
panelLayout.setHgap(10);
panelLayout.setVgap(10);
final JPanel panel = new JPanel(panelLayout);
//location label
JLabel locationLabel = new JLabel("Location");
panel.add(locationLabel);
//text Field for location
final JTextField locationText = new JTextField();
panel.add(locationText);
//month label
JLabel monthLabel = new JLabel("Month of the Year");
panel.add(monthLabel);
//comboBox for months
String[] months = new String[] {"","January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
JComboBox<String> monthList = new JComboBox<>(months);
panel.add(monthList);
//JCheckBox
JCheckBox celsiusCheck = new JCheckBox("Show Celsius Temperature");
panel.add(celsiusCheck);
//Button inside button pane
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 150, 0, 0));
JButton goButton = new JButton("Go");
buttonPane.add(goButton);
//JPanel for display
JPanel displayPanel = new JPanel();
displayPanel.setBorder(BorderFactory.createLineBorder(Color.black));
//Create and setup window
JFrame frame = new JFrame("Midterm Project");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(3, 1));
mainPanel.add( panel );
mainPanel.add( buttonPane );
mainPanel.add( displayPanel );
//Set up the content pane.
mainPanel.setOpaque(true); //content panes must be opaque
frame.setContentPane(mainPanel);
//Display the window.
frame.pack();
frame.setVisible(true);
}//createAndShowGUI()
class displayPanel extends JPanel {
double fahrenheit = generateRand();
//double celsius = getCelsius(fahrenheit);
String celsius = Double.toString(getCelsius(fahrenheit));
@Override
public void paintComponent(Graphics g) {
g.drawString("Hello", 10, 10);
}//paintComponent();
}
答案 0 :(得分:0)
.box {
display: table;
margin-right: auto;
margin-left: auto;
width: 100%;
}
.rec {
display: table-cell;
padding: 10px;
text-align: center;
background: red;
}
.navigation {
display: table-cell;
background: yellow;
}
.nagivation.next > a {
vertical-align: right;
}
应该......
JPanel displayPanel = new JPanel();
从它的外观来看!
displayPanel被更改为displayPanelVar,因为变量名不能与类相同,可能值得将displayPanel类名重命名为DisplayPanel