java新手,我无法理解为什么我的动作监听器不能在jcombobox上工作。我想我已经按照网络上的其他示例来访问getSelectedItem,但没有任何事情发生。 仅供参考,我的项目是一个单位转换器(使用MVC ......但是,这不是我的优先考虑)。 非常感谢任何帮助。 谢谢,西蒙。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class UnitConverterView extends JFrame{
//variables and components
private static final long serialVersionUID = -4673040337179571462L;
private JComboBox<String> unitCategory;
private JTextField fromValue = new JTextField(7);
private JComboBox<String> convertFrom;
private JLabel equalsLabel = new JLabel(" = ");
private JTextField toValue = new JTextField(7);
private JComboBox<String> convertTo;
//constructor
UnitConverterView(){
//set up the view and components
JPanel unitPanel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,300);
String[] categories = {"Length","Weight","Speed","Temperature"};
unitCategory = new JComboBox<>(categories);
String[] tofromValues = {" "};
convertFrom = new JComboBox<>(tofromValues);
convertTo = new JComboBox<>(tofromValues);
unitPanel.add(unitCategory);
unitPanel.add(fromValue);
unitPanel.add(convertFrom);
unitPanel.add(equalsLabel);
unitPanel.add(toValue);
unitPanel.add(convertTo);
this.add(unitPanel);
}
//get value to convert from
public int getMeasurement() {
return Integer.parseInt(fromValue.getText());
}
//listen for unitCategory to be selected
void addUnitCategoryListener(ActionListener listenForUnitCategory) {
unitCategory.addActionListener(listenForUnitCategory);
}
class UnitCatListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
/*String unitSelected = (String) unitCategory.getSelectedItem();
if (e.getSource() == unitCategory) {
String unitName = (String) unitCategory.getSelectedItem();
System.out.println("UnitName = " + unitName);
changeText(unitName);
}*/
JComboBox cb = (JComboBox)e.getSource();
String unitName = (String) cb.getSelectedItem();
System.out.println("UnitName = " + unitName);
}
void changeText(String name) {
toValue.setText(name);
}
}
}
答案 0 :(得分:1)
您已声明了一个方法addUnitCategoryListener(),用于将侦听器注册到组合框,但您从未调用此方法。这就是听众从未注册的原因。
在构造函数的末尾添加以下行,那么你应该没问题:
addUnitCategoryListener(new UnitCatListener());
答案 1 :(得分:1)
要简单地解决您的问题,请调用您创建的方法在组件上注册侦听器。将其添加到构造函数中:
<?php
include("../db_connect.php");
$sql="select * from notifications where userid='$uid' or rec_id='$uid'";
$result=mysqli_query($con,$sql);
?>
<div class="col-sm-10">
<table class="table table-striped ">
<thead style="background-color:skyblue;">
<td></td><td></td><td></td><td></td><td><h3><i class="fa fa-cogs"></i>Notifications</h3></td><td></td><td></td><td></td></thead>
<thead style="background-color:wheet;" class="table-bordered">
<tr>
<td><b>Sr.No.</b></td>
<td><b>From</b></td>
<td><b>Recevier</b></td>
<td><b>Amount</b></td>
<td><b>Details</b></td>
<td><b>Date</b></td>
<td><b>User Info</b></td>
<td><b>Status</b></td>
</tr>
</thead>
<tbody class="table-bordered ">
<?php
if($result)
{
$i=1;
while($row = $result->fetch_assoc()) {
$sql12="select * from users inner join bank_details on users.userid=bank_details.userid where users.userid='".$row['rec_id']."'";
$result12=mysqli_query($con,$sql12);
$row1 = $result12->fetch_assoc();
echo "<tr><td>".$i."</td><td>".$row['userid']."</td><td>".$row['rec_id']."</td><td>".$row['amount']."</td><td>".$row['details']."</td><td>".$row['date1']."</td>";
if($uid == $row['rec_id'])
{
echo "<td><button class='btn btn-warning' disabled data-toggle='modal' data-target='#myModal".$i."'>View</button></td>";
}
else
{
echo "<td><button class='btn btn-success' data-toggle='modal' data-target='#myModals".$i."'>View</button></td>";
}
if($uid == $row['userid'])
{
if($row['status']=="pending")
{
echo "<td><button class='btn btn-info' disabled id='sndr'>Pending to Accept</button></td></tr>";
}
else if($row['status']=="accepted")
{
echo "<td><button class='btn btn-success' disabled id='sndr'>Request Accepted</button></td></tr>";
}
else
{
echo "<td><button class='btn btn-danger' disabled id='sndr'>Request Rejected</button></td></tr>";
}
}
else
{
if($row['status']=="pending" )
{
?>
<td>
<form role="form">
<div class="form-group">
<select class="form-control" id="sel1" >
<option>Select</option>
<option value="accepted" onclick="aprvs(<?php echo $row['id']; ?>, this.value);"><span class="btn btn-success"> Accept</span></option>
<option value="rejected" onclick="aprvs(<?php echo $row['id']; ?>, this.value);"><span class="btn btn-danger">Reject</span></option>
</select>
</div>
</form>
</td>
</tr>
<?php
}
else if($row['status']=="accepted")
{
echo "<td><button class='btn btn-success' disabled id='sndr'>Request Accepted</button></td></tr>";
}
else{
echo "<td><button class='btn btn-danger' disabled id='sndr'>Request Rejected</button></td></tr>";
}
}
?>
<div class='modal fade' id='<?php echo "myModals".$i.""; ?>' role='dialog' tabindex="-1" aria-hidden="true">
<div class="modal-dialog" >
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="background-color:green;padding:10px;">
<button type="button" class="close" data-dismiss="modal" >×</button>
<center>
<h4 class="modal-title" align="center" style="color:white;">User Details</h4>
</center>
</div>
<div class="modal-body" style="padding:10px;">
<table class='table table-striped'>
<thead>
<tr><td><h4 style='color:blue;'></b>User Details:</b></h4></td><td></td></tr>
</thead>
<tbody>
<tr><td><b>User Id:</b></td><td><?php echo $row1['userid']; ?></td></tr>
<tr><td><b>Name:</b></td><td><?php echo $row1['name']; ?></td></tr>
<tr><td><b>Father Name:</b></td><td><?php echo $row1['fname']; ?></td></tr>
</tbody>
<!--
<thead><tr><td><h4 style='color:blue;'></b>Bank Details:</b></h4></td><td></td></tr></thead>
<tbody>
<tr><td><b>Bank Name:</b></td><td><?php echo $row1['bname']; ?></td></tr>
<tr><td><b>Branch:</b></td><td><?php echo $row1['branch']; ?></td></tr>
<tr><td><b>A/c. No.:</b></td><td><?php echo $row1['account_no']; ?></td></tr>
<tr><td><b>IFSC Code:</b></td><td><?php echo $row1['ifs']; ?></td></tr>
<tbody>
<thead><tr><td><h4 style='color:blue;'></b>Contact Details:</b></h4></td><td></td></tr></thead>
<tbody>
<tr><td><b>Mobile No.:</b></td><td><?php echo $row1['mob']; ?></td></tr>
<tr><td><b>Alternate No.:</b></td><td><?php echo $row1['alt']; ?></td></tr>
<tr><td><b>E-Mail:</b></td><td><?php echo $row1['email']; ?></td></tr>
</tbody>-->
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
<?php
echo "</div>";
$i++;
}
}
else
{
echo "<tr><td>No Notifications to show</td></tr>";
}
?>
</tbody>
</table>
<?php
}
?>
但是,您可能想知道一些事情:
addUnitCategoryListener(new UnitCatListener());
,ItemListener
通常比ActionListener
做得更好。如果用户选择已经选择的项目(基本上什么都不做),前一个不会触发事件。通常在这些情况下你不需要做任何事情。你不需要额外的方法来注册监听器,你可以直接添加到你的构造函数行
JComboBox
并删除自定义方法。
unitCategory.addActionListener(new UnitCatListener());
和changeText
。getMeasurement
使用JComboBox
。JComboBox<String>
作为字段 - 本地变量将会这样做 - 因为您以后不需要在任何地方引用它(除非您计划在运行时更改标签的属性)